What's the fastest way to alphabetize your bookshelf? - Chand John
3,890,933 views ・ 2016-11-28
请双击下面的英文字幕来播放视频。
翻译人员: Kai Ma
校对人员: Mingyu Cui
00:06
You work at the college library.
0
6911
2266
你是一名大学图书馆的管理员,
00:09
You're in the middle of a quiet afternoon
1
9177
2220
正在享受午后的闲暇时光。
00:11
when suddenly a shipment of 1,280
different books arrives.
2
11397
6614
这时快递突然送来了1280本各式各样的书。
00:18
The books have been dropped of
in one long straight line,
3
18011
3679
这些书被排成长长的一排,
00:21
but they're all out of order,
4
21690
1598
毫无规律,
00:23
and the automatic sorting
system is broken.
5
23288
3743
自动分类系统又恰好出现了故障。
00:27
To make matters worse,
classes start tomorrow,
6
27031
2636
更糟糕的是,明天一早,
00:29
which means that
first thing in the morning,
7
29667
2338
前来上课的学生们,
00:32
students will show up in droves
looking for these books.
8
32005
4552
就会涌入图书馆借书。
00:36
How can you get them all sorted in time?
9
36557
2936
如何才能在规定的时间内
整理好所有书籍?
00:39
One way would be to start at one end
of the line with the first pair of books.
10
39493
5286
一种方法是
从最前面的两本书开始。
00:44
If the first two books are in order,
then leave them as they are.
11
44779
3847
如果这两本书位置正确
则不做任何改动,
00:48
Otherwise, swap them.
12
48626
2294
否则,将它们对换。
00:50
Then, look at the second and third books,
13
50920
2296
然后再看第二本书和第三本书,
00:53
repeat the process,
14
53216
1663
重复这个操作,
00:54
and continue until you reach
the end of the line.
15
54879
3056
直到你排列至最后一本书。
00:57
At some point, you'll come across
the book that should be last,
16
57935
3250
当你遇到应该位于末位的那本书,
01:01
and keep swapping it with every
subsequent book,
17
61185
3525
那么只需要将它不断地与后一本书交换,
01:04
moving it down the line until it
reaches the end where it belongs.
18
64710
4570
直至到达队伍末尾。
01:09
Then, start from the beginning
and repeat the process
19
69280
2945
然后,再从头开始,
01:12
to get the second to last book
in its proper place,
20
72225
3285
就可以找出倒数第二本书。
01:15
and keep going until all books are sorted.
21
75510
3311
如此循环,直到将所有的书归位。
01:18
This approach is called Bubble Sort.
22
78821
3041
这个方法被称为 “冒泡排序”。
01:21
It's simple but slow.
23
81862
2294
这个方法非常简单,但效率低下。
01:24
You'd make 1,279 comparisons
in the first round,
24
84156
5175
在第一次循环中,
你需要进行1279次对比,
01:29
then 1,278, and so on,
25
89331
4292
第二次循环则需要1278次,
01:33
adding up to 818,560 comparisons.
26
93623
4919
总计818560次对比。
01:38
If each took just one second,
the process would take over nine days.
27
98542
5731
按每次比较需要1秒,
这一过程将长达9天。
01:44
A second strategy would be to start
by sorting just the first two books.
28
104273
4296
第二种方法是
先将前两本排序。
01:48
Then, take the third book and compare it
with the book in the second spot.
29
108569
5164
然后,将第三本书和第二本进行排列。
01:53
If it belongs before the second book,
swap them,
30
113733
3440
如果第三本应位于第二本之前,则交换它们的位置。
01:57
then compare it
with the book in the first spot,
31
117173
2468
再它和第一本书进行对比,
01:59
and swap again if needed.
32
119641
2041
如果这本书应位于第一本之前,则交换它们的位置。
02:01
Now you've sorted the first three books.
33
121682
2198
现在,你就完成了前三本的排列。
02:03
Keep adding one book at a time
to the sorted sub-line,
34
123880
3852
每次将一本书
插入之前已经排列好的队伍中,
02:07
comparing and swapping the new book
with the one before it
35
127732
4077
与前面相邻的一本书进行对比
并判断是否交换,
02:11
until it's correctly placed
among the books sorted so far.
36
131809
4195
直到这本书到达队伍中正确的位置。
02:16
This is called Insertion Sort.
37
136004
2209
这种方法被称为 “插入排序”。
02:18
Unlike Bubble Sort, it usually doesn't
require comparing every pair of books.
38
138213
4731
与“冒泡排序”不同的是,
这种方法通常不必比对所有相邻的书。
02:22
On average, we'd expect to only need
to compare each book
39
142944
4010
一般来说,
每本书被对比的次数
02:26
to half of the books that came before it.
40
146954
2460
只有前者的一半。
02:29
In that case, the total
number of comparisons
41
149414
2709
这样,总共的对比次数
02:32
would be 409,280,
42
152123
3860
也就是409280次。
02:35
taking almost five days.
43
155983
2152
需要将近5天的时间。
02:38
You're still doing
way too many comparisons.
44
158135
2489
对比的次数还是太多。
02:40
Here's a better idea.
45
160624
1891
还有一种更好的办法:
02:42
First, pick a random book.
46
162515
2370
首先,随机抽出一本书,
02:44
Call it the partition and compare it
to every other book.
47
164885
4721
称之为“分界点”,
然后将每本书都与之进行对比。
02:49
Then, divide the line
48
169606
1909
然后,根据“分界点”,
02:51
by placing all the books
that come before the partition on its left
49
171515
4151
把所有应放在它之前的书放在左边,
02:55
and all the ones that come after it
on its right.
50
175666
3159
其他的放在右边。
02:58
You've just saved loads of time
51
178825
1590
这样你就已经节省了大量的时间,
03:00
by not having to compare
any of the books on the left
52
180415
3430
因为你再也不必将左边的书,
03:03
to any of the ones
on the right ever again.
53
183845
3400
与右边的任何书进行比较。
03:07
Now, looking only
at the books on the left,
54
187245
2420
现在,只看左边的一部分。
03:09
you can again pick a random partition book
55
189665
2877
你可以从中再随机抽出一本书,
03:12
and separate those books that come
before it from those that come after it.
56
192542
4724
然后以此为另一个“分界点”。
03:17
You can keep creating
sub-partitions like this
57
197266
2470
你可以像这样不断地进行分割,
产生新的“分界点”,
03:19
until you have a bunch of small sub-lines,
58
199736
2648
直到得出许多的分支。
03:22
each of which you'd sort quickly using
another strategy, like Insertion Sort.
59
202384
5380
每一个分支都可以用其他的方法进行重排,
比如 “插入排序”。
03:27
Each round of partitioning requires
about 1,280 comparisons.
60
207764
5162
每次“分界点”循环
所需要的对比次数大约是1280次。
03:32
If your partitions are pretty balanced,
61
212926
2540
如果你的“分界点”分布十分均匀,
03:35
dividing the books into 128 sub-lines of ten
would take about seven rounds,
62
215466
5790
那么把整列书分成128个分支,每个10本,
大约只需要7次循环,
03:41
or 8,960 seconds.
63
221256
2691
也就是8960秒。
03:43
Sorting these sub-lines would add
about 22 seconds each.
64
223947
4789
将每个分支进行排序,
每个约需22秒。
03:48
All in all, this method
known as QuickSort
65
228736
3081
这个方法被称为 “快速排序”。
03:51
could sort the books
in under three and a half hours.
66
231817
3066
它能将所有的书
在三个半小时之内排列整齐。
03:54
But there's a catch.
67
234883
1114
但是也有特例。
03:55
Your partitions could end up lopsided,
saving no time at all.
68
235997
3578
取出的分界点可能全都挤在一端,
这样你不会节省任何时间。
03:59
Luckily, this rarely happens.
69
239575
1902
幸运的是,这种情况几乎不可能发生。
04:01
That's why QuickSort is one of the most
efficient strategies
70
241477
3433
这也使得“快速排序”
04:04
used by programmers today.
71
244910
2006
成为编程中最高效的排序方法之一。
04:06
They use it for things like sorting items
in an online store by price,
72
246916
3931
例如,它被用于网络购物中的价格排序,
04:10
or creating a list of all the gas stations
close to a given location
73
250847
4011
或是按照距离
04:14
sorted by distance.
74
254858
1521
依次列举出最近的加油站地址。
04:16
In your case, you're done quick sorting
with time to spare.
75
256379
4028
在给书排序的例子中,
“快速排序”为你节省了大量的时间。
04:20
Just another high-stakes day
in the library.
76
260407
2261
多么充满乐趣的一天。
New videos
Original video on YouTube.com
关于本网站
这个网站将向你介绍对学习英语有用的YouTube视频。你将看到来自世界各地的一流教师教授的英语课程。双击每个视频页面上显示的英文字幕,即可从那里播放视频。字幕会随着视频的播放而同步滚动。如果你有任何意见或要求,请使用此联系表与我们联系。