What's an algorithm? - David J. Malan
你的大脑也能运行算法 - David J. Malan
2,582,025 views ・ 2013-05-20
请双击下面的英文字幕来播放视频。
00:00
Translator: Andrea McDonough
Reviewer: Jessica Ruby
0
0
7000
翻译人员: Judy Z
校对人员: Catherine Gu
00:15
What's an algorithm?
1
15483
1348
什么是算法?
00:16
In computer science,
2
16831
831
在计算机科学中,
00:17
an algorithm is a set of instructions
3
17662
1754
算法是一系列的指令,
00:19
for solving some problem, step-by-step.
4
19416
2689
用来一步一步地解决问题。
00:22
Typically, algorithms are executed by computers,
5
22105
2272
通常,算法由计算机执行,
00:24
but we humans have algorithms as well.
6
24377
2167
但是我们人类同样可以用算法。
00:26
For instance, how would you go about counting
7
26544
1853
例如,你会怎么去数
00:28
the number of people in a room?
8
28397
1820
一个房间中的人数?
00:30
Well, if you're like me,
9
30217
1215
好吧,如果你像我一样,
00:31
you probably point at each person,
10
31432
1496
你可能会去数每一个人,
00:32
one at a time,
11
32928
960
一次数一个,
00:33
and count up from 0:
12
33888
1837
从0开始:
00:35
1, 2, 3, 4 and so forth.
13
35725
2873
1, 2, 3, 4...
00:38
Well, that's an algorithm.
14
38598
1113
那么这就是算法。
00:39
In fact, let's try to express it
15
39711
1134
事实上,让我们尝试这样去表达它,
00:40
a bit more formally in pseudocode,
16
40845
2229
用有点儿更正式的伪代码,
00:43
English-like syntax
17
43074
831
00:43
that resembles a programming language.
18
43905
2124
像英文的句法
类似于编程语言。
00:46
Let n equal 0.
19
46029
1767
设n等于0。
00:47
For each person in room, set n = n + 1.
20
47796
4792
对于房间中的每个人,让n=n+1。
00:52
How to interpret this pseudocode?
21
52588
1497
如何来理解这些伪代码呢?
00:54
Well, line 1 declares, so to speak,
22
54085
1836
好吧,第一行,可以这么说,它声明了
00:55
a variable called n
23
55921
1416
变量n
00:57
and initializes its value to zero.
24
57337
2379
并且将它初始化为零。
00:59
This just means that at the beginning of our algorithm,
25
59716
2335
这仅表示我们的算法在开始时
01:02
the thing with which we're counting
26
62051
1584
我们计算的对象
01:03
has a value of zero.
27
63635
1668
的值为零。
01:05
After all, before we start counting,
28
65303
1336
毕竟,在我们开始数之前,
01:06
we haven't counted anything yet.
29
66639
1669
我们没有计数任何对象。
01:08
Calling this variable n is just a convention.
30
68308
2248
将这个变量称为n只是一个惯例。
01:10
I could have called it almost anything.
31
70556
2005
我几乎可任意称它。
01:12
Now, line 2 demarks the start of loop,
32
72561
2086
在第二行中,标志了循环的开始,
01:14
a sequence of steps that will repeat some number of times.
33
74647
3044
这里将重复执行一系列的步骤,
01:17
So, in our example, the step we're taking
34
77691
1794
所以在我们的例子中我们所说的步骤
01:19
is counting people in the room.
35
79485
1734
是数房间中的人数。
01:21
Beneath line 2 is line 3,
36
81219
1763
接着第二行的是第三行,
01:22
which describes exactly how we'll go about counting.
37
82982
2511
它描述了我们究竟怎样去计数。
01:25
The indentation implies that it's line 3
38
85493
2250
行首缩进表明这是第三行
01:27
that will repeat.
39
87743
1222
这个过程会重复执行。
01:28
So, what the pseudocode is saying
40
88965
1219
那么,再来看看伪代码表达了什么,
01:30
is that after starting at zero,
41
90184
2066
在从零开始后,
01:32
for each person in the room,
42
92250
1710
对于房间中的每一个人,
01:33
we'll increase n by 1.
43
93960
2218
我们给n加1。
01:36
Now, is this algorithm correct?
44
96178
2329
那么,这种算法正确吗?
01:38
Well, let's bang on it a bit.
45
98507
1608
我们继续来讨论这个话题。
01:40
Does it work if there are 2 people in the room?
46
100115
2826
如果房间中有两个人,这个算法有效吗?
01:42
Let's see.
47
102941
780
我们来看一看。
01:43
In line 1, we initialize n to zero.
48
103721
2085
在第一行,我们初始化n为零。
01:45
For each of these two people,
49
105806
1302
对于两人中的每一个人,
01:47
we then increment n by 1.
50
107108
2168
我们把n增加1。
01:49
So, in the first trip through the loop,
51
109276
1582
所以在第一次循环时,
01:50
we update n from zero to 1,
52
110858
2005
我们将n的值从零更新为1,
01:52
on the second trip through that same loop,
53
112863
1655
在第二次循环时,
01:54
we update n from 1 to 2.
54
114518
2249
我们将n的值由1更新为2。
01:56
And so, by this algorithm's end, n is 2,
55
116767
3341
在这个算法结束时,n的值是2,
02:00
which indeed matches the number of people in the room.
56
120108
2113
可以看到,n的值确实等于房间中人数。
02:02
So far, so good.
57
122221
1223
到目前为止,一切都好。
02:03
How about a corner case, though?
58
123444
1752
那么考虑另一种极端情况下算法会怎样?
02:05
Suppose that there are zero people in the room,
59
125196
1964
除了计数的我以外,
02:07
besides me, who's doing the counting.
60
127160
2347
房间里没有其他任何人。
02:09
In line 1, we again initialize n to zero.
61
129507
3010
在第一行中,我们仍然将n初始化为零。
02:12
This time, though, line 3 doesn't execute at all
62
132517
2522
然而,这一次,第三行根本不执行
02:15
since there isn't a person in the room,
63
135039
1880
因为房间中没有其他人,
02:16
and so, n remains zero,
64
136919
1624
所以,n仍然为零,
02:18
which indeed matches the number of people in the room.
65
138543
2359
同样等于房间中的人数,
02:20
Pretty simple, right?
66
140902
906
是不是很简单?
02:21
But counting people one a time is pretty inefficient, too, no?
67
141808
3216
但是,每次只数一个人是不是也很没效率?
02:25
Surely, we can do better!
68
145024
1568
当然,我们可以做得更好!
02:26
Why not count two people at a time?
69
146592
1866
为何一次不连数两个人?
02:28
Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth,
70
148458
5099
区别于1,2,3,4,5,6,7,8这样去数
02:33
why not count
71
153557
918
为何不
02:34
2, 4, 6, 8, and so on?
72
154475
2127
2,4,6,8...这样呢?
02:36
It even sounds faster, and it surely is.
73
156602
2418
它甚至听起来比之前的方法更快,它肯定是。
02:39
Let's express this optimization in pseudocode.
74
159020
2835
让我们把这个优化过程用伪代码的形式表示出来。
02:41
Let n equal zero.
75
161855
1462
设n等于零。
02:43
For each pair of people in room,
76
163317
1997
对于房间中的每一对人,
02:45
set n = n + 2.
77
165314
2588
设n=n+2。
02:47
Pretty simple change, right?
78
167902
1673
很简单地改动,是吗?
02:49
Rather than count people one at a time,
79
169575
1791
不是一次数一个人,
02:51
we instead count them two at a time.
80
171366
2343
我们一次数两个人。
02:53
This algorithm's thus twice as fast as the last.
81
173709
2815
所以这个算法比之前的算法快了两倍。
02:56
But is it correct?
82
176524
1346
但是它正确么?
02:57
Let's see.
83
177870
794
让我们来看看。
02:58
Does it work if there are 2 people in the room?
84
178664
2125
假设房间中有两个人时算法正常工作吗?
03:00
In line 1, we initialize n to zero.
85
180789
2342
在第一行中,我们把n初始化为零。
03:03
For that one pair of people, we then increment n by 2.
86
183131
3268
对于这一对人,我们把n增加2。
03:06
And so, by this algorithm's end, n is 2,
87
186399
2522
所以,当算法结束时,n的值是2,
03:08
which indeed matches the number of people in the room.
88
188921
2382
它正好等于房间中的人数。
03:11
Suppose next that there are zero people in the room.
89
191303
2412
进一步假设房间中人数是0。
03:13
In line 1, we initialize n to zero.
90
193715
2587
在第一行中,我们把n初始化为零。
03:16
As before, line 3 doesn't execute at all
91
196302
2080
像之前一样,第三行根本不执行,
03:18
since there aren't any pairs of people in the room,
92
198382
2342
因为房间中没有成对的人,
03:20
and so, n remains zero,
93
200724
1686
所以,n仍为零,
03:22
which indeed matches the number of people in the room.
94
202410
2773
这也同样符合房间中的人数。
03:25
But what if there are 3 people in the room?
95
205183
2372
但要是房间中有三个人时情况会怎样呢?
03:27
How does this algorithm fair?
96
207555
1937
这个算法会怎么公平处理?
03:29
Let's see.
97
209492
829
让我们来看一看。
03:30
In line 1, we initialize n to zero.
98
210321
2670
这三人中的两人组成一对
03:32
For a pair of those people,
99
212991
1290
在第一行中,我们把n初始化为0,
03:34
we then increment n by 2,
100
214281
1916
我们把n增加2,
03:36
but then what?
101
216197
992
但是然后情况如何?
03:37
There isn't another full pair of people in the room,
102
217189
2262
房间里剩下的人中没有足够的人凑成一对,
03:39
so line 2 no longer applies.
103
219451
2210
所以第二行不再执行。
03:41
And so, by this algorithm's end,
104
221661
1669
所以,到这里算法结束,
03:43
n is still 2, which isn't correct.
105
223330
2596
这里n仍然是2,这是不正确的。
03:45
Indeed this algorithm is said to be buggy
106
225926
2332
事实上,这样的算法就是被称为有缺陷的,
03:48
because it has a mistake.
107
228258
1148
因为它有一个错误。
03:49
Let's redress with some new pseudocode.
108
229406
1896
让我们用新的伪代码来纠正这一错误。
03:51
Let n equal zero.
109
231302
1793
让n等于0。
03:53
For each pair of people in room,
110
233095
2123
对于房间中的每一对人,
03:55
set n = n + 2.
111
235218
2422
设n=n+2。
03:57
If 1 person remains unpaired,
112
237640
2459
如果有一个落单的人,
04:00
set n = n + 1.
113
240099
2376
设n=n+1。
04:02
To solve this particular problem,
114
242475
1507
为了解决这个特别的难题,
04:03
we've introduced in line 4 a condition,
115
243982
2249
我们在第四行引入了一个条件,
04:06
otherwise known as a branch,
116
246231
1835
或称为一个分支,
04:08
that only executes if there is one person
117
248066
2253
当只有一个人
04:10
we could not pair with another.
118
250319
1876
无法配对时执行。
04:12
So now, whether there's 1 or 3
119
252195
2065
所以,现在无论是1或3,
04:14
or any odd number of people in the room,
120
254260
2273
或其它任意的奇数人数,
04:16
this algorithm will now count them.
121
256533
2288
这个算法都能计数他们。
04:18
Can we do even better?
122
258821
1345
我们能不能做得更好?
04:20
Well, we could count in 3's or 4's or even 5's and 10's,
123
260166
3294
我们可以3个、4个或甚至5个、10个的数,
04:23
but beyond that it's going to get
124
263460
1300
但是更多的人
04:24
a little bit difficult to point.
125
264760
1870
会使这在表达上有点儿困难。
04:26
At the end of the day,
126
266630
937
在最后
04:27
whether executed by computers or humans,
127
267567
2264
是用计算机还是由人类执行,
04:29
algorithms are just a set of instructions
128
269831
1960
算法只是一组指令,
04:31
with which to solve problems.
129
271791
1838
通过使用它我们解决难题。
04:33
These were just three.
130
273629
1743
这些只是三种情况。
04:35
What problem would you solve with an algorithm?
131
275372
2982
你会用算法解决什么问题?
New videos
关于本网站
这个网站将向你介绍对学习英语有用的YouTube视频。你将看到来自世界各地的一流教师教授的英语课程。双击每个视频页面上显示的英文字幕,即可从那里播放视频。字幕会随着视频的播放而同步滚动。如果你有任何意见或要求,请使用此联系表与我们联系。