Whats an algorithm David J. Malan

Translator: Andrea McDonough
Reviewer: Jessica Ruby

What’s an algorithm?

In computer science,

an algorithm is a set of instructions

for solving some problem, step-by-step.

Typically, algorithms are executed by computers,

but we humans have algorithms as well.

For instance, how would you go about counting

the number of people in a room?

Well, if you’re like me,

you probably point at each person,

one at a time,

and count up from 0:

1, 2, 3, 4 and so forth.

Well, that’s an algorithm.

In fact, let’s try to express it

a bit more formally in pseudocode,

English-like syntax

that resembles a programming language.

Let n equal 0.

For each person in room, set n = n + 1.

How to interpret this pseudocode?

Well, line 1 declares, so to speak,

a variable called n

and initializes its value to zero.

This just means that at the beginning of our algorithm,

the thing with which we’re counting

has a value of zero.

After all, before we start counting,

we haven’t counted anything yet.

Calling this variable n is just a convention.

I could have called it almost anything.

Now, line 2 demarks the start of loop,

a sequence of steps that will repeat some number of times.

So, in our example, the step we’re taking

is counting people in the room.

Beneath line 2 is line 3,

which describes exactly how we’ll go about counting.

The indentation implies that it’s line 3

that will repeat.

So, what the pseudocode is saying

is that after starting at zero,

for each person in the room,

we’ll increase n by 1.

Now, is this algorithm correct?

Well, let’s bang on it a bit.

Does it work if there are 2 people in the room?

Let’s see.

In line 1, we initialize n to zero.

For each of these two people,

we then increment n by 1.

So, in the first trip through the loop,

we update n from zero to 1,

on the second trip through that same loop,

we update n from 1 to 2.

And so, by this algorithm’s end, n is 2,

which indeed matches the number of people in the room.

So far, so good.

How about a corner case, though?

Suppose that there are zero people in the room,

besides me, who’s doing the counting.

In line 1, we again initialize n to zero.

This time, though, line 3 doesn’t execute at all

since there isn’t a person in the room,

and so, n remains zero,

which indeed matches the number of people in the room.

Pretty simple, right?

But counting people one a time is pretty inefficient, too, no?

Surely, we can do better!

Why not count two people at a time?

Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth,

why not count

2, 4, 6, 8, and so on?

It even sounds faster, and it surely is.

Let’s express this optimization in pseudocode.

Let n equal zero.

For each pair of people in room,

set n = n + 2.

Pretty simple change, right?

Rather than count people one at a time,

we instead count them two at a time.

This algorithm’s thus twice as fast as the last.

But is it correct?

Let’s see.

Does it work if there are 2 people in the room?

In line 1, we initialize n to zero.

For that one pair of people, we then increment n by 2.

And so, by this algorithm’s end, n is 2,

which indeed matches the number of people in the room.

Suppose next that there are zero people in the room.

In line 1, we initialize n to zero.

As before, line 3 doesn’t execute at all

since there aren’t any pairs of people in the room,

and so, n remains zero,

which indeed matches the number of people in the room.

But what if there are 3 people in the room?

How does this algorithm fair?

Let’s see.

In line 1, we initialize n to zero.

For a pair of those people,

we then increment n by 2,

but then what?

There isn’t another full pair of people in the room,

so line 2 no longer applies.

And so, by this algorithm’s end,

n is still 2, which isn’t correct.

Indeed this algorithm is said to be buggy

because it has a mistake.

Let’s redress with some new pseudocode.

Let n equal zero.

For each pair of people in room,

set n = n + 2.

If 1 person remains unpaired,

set n = n + 1.

To solve this particular problem,

we’ve introduced in line 4 a condition,

otherwise known as a branch,

that only executes if there is one person

we could not pair with another.

So now, whether there’s 1 or 3

or any odd number of people in the room,

this algorithm will now count them.

Can we do even better?

Well, we could count in 3’s or 4’s or even 5’s and 10’s,

but beyond that it’s going to get

a little bit difficult to point.

At the end of the day,

whether executed by computers or humans,

algorithms are just a set of instructions

with which to solve problems.

These were just three.

What problem would you solve with an algorithm?

译者:Andrea McDonough
审稿人:Jessica Ruby

什么是算法?

在计算机科学中

,算法是一组

用于逐步解决某些问题的指令。

通常,算法是由计算机执行的,

但我们人类也有算法。

例如,您将如何计算

房间里的人数?

好吧,如果你像我一样,

你可能会指向每个人,

一次一个,

然后从 0 开始数:

1、2、3、4 等等。

嗯,这是一个算法。

事实上,让我们尝试用类似于编程语言的类似英语

的伪代码更正式地表达它

令 n 等于 0。

对于房间里的每个人,设置 n = n + 1。

如何解释这个伪代码?

好吧,可以说,第 1 行声明

了一个名为 n 的变量

并将其值初始化为零。

这只是意味着在我们的算法开始

时,我们正在计算的东西

的值为零。

毕竟,在我们开始数之前,

我们还没有数过任何东西。

调用这个变量 n 只是一个约定。

我几乎可以称它为任何东西。

现在,第 2 行标记了循环的开始,这

是一系列将重复若干次的步骤。

因此,在我们的示例中,我们正在采取的步骤

是计算房间里的人数。

第 2 行下面是第 3 行,

它准确地描述了我们将如何进行计数。

缩进意味着将重复第 3 行

所以,伪代码的意思

是,在从零开始之后,

对于房间里的每个人,

我们将 n 增加 1。那么

,这个算法是否正确?

好吧,让我们来看看它。

如果房间里有 2 个人,它可以工作吗?

让我们来看看。

在第 1 行,我们将 n 初始化为零。

对于这两个人中的每一个人,

我们将 n 加 1。

因此,在循环的第一次旅行中,

我们将 n 从零更新为 1,

在同一循环的第二次旅行中,

我们将 n 从 1 更新为 2。

并且 所以,到这个算法结束时,n 是 2,

这确实与房间里的人数相匹配。

到现在为止还挺好。

但是,角落案例怎么样?

假设房间里

除了我之外有零个在数数的人。

在第 1 行,我们再次将 n 初始化为零。

不过这一次,第 3 行根本没有执行,

因为房间里没有人

,所以 n 保持为零,

这确实与房间里的人数相匹配。

很简单,对吧?

但是,一次数一个人也是非常低效的,不是吗?

当然,我们可以做得更好!

为什么不一次数两个人?

与其数 1、2、3、4、5、6、7、8 等等,

为什么不数

2、4、6、8 等等?

它甚至听起来更快,而且确实如此。

让我们用伪代码来表达这种优化。

让 n 等于零。

对于房间里的每一对人,

设置 n = n + 2。

很简单的改变,对吧?

我们不是一次数一个人,

而是一次数两个人。

因此,该算法的速度是上一个算法的两倍。

但它是正确的吗?

让我们来看看。

如果房间里有 2 个人,它可以工作吗?

在第 1 行,我们将 n 初始化为零。

对于那一对人,我们将 n 增加

2。因此,在该算法结束时,n 为 2,

这确实与房间中的人数相匹配。

接下来假设房间里的人为零。

在第 1 行,我们将 n 初始化为零。

和以前一样,第 3 行根本不执行,

因为房间里没有任何人

,所以 n 保持为零,

这确实与房间里的人数相匹配。

但是如果房间里有 3 个人呢?

这个算法如何公平?

让我们来看看。

在第 1 行,我们将 n 初始化为零。

对于其中的一对人,

我们将 n 增加 2,

但是然后呢?

房间里没有另一对完整的人,

所以第 2 行不再适用。

所以,到这个算法结束时,

n 仍然是 2,这是不正确的。

事实上,据说这个算法有问题,

因为它有一个错误。

让我们用一些新的伪代码来补救。

让 n 等于零。

对于房间里的每一对人,

设置 n = n + 2。

如果 1 人仍未配对,则

设置 n = n + 1。

为了解决这个特殊问题,

我们在第 4 行引入了一个条件,

也称为分支,

仅当有一个

人无法与另一个人配对时才会执行。

所以现在,无论房间里有 1 人还是 3 人

或任何奇数人,

这个算法现在都会计算他们。

我们还能做得更好吗?

好吧,我们可以计算 3 或 4 甚至 5 和 10,

但除此之外,它会变得

有点难以指出。

归根结底,

无论是由计算机还是人类执行,

算法都只是一组

用于解决问题的指令。

这只是三个。

你会用算法解决什么问题?