What's the fastest way to alphabetize your bookshelf? - Chand John
3,890,933 views ・ 2016-11-28
請雙擊下方英文字幕播放視頻。
譯者: kejia chen
審譯者: 庭芝 梁
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
所以總共要比較 818,560 次。
01:38
If each took just one second,
the process would take over nine days.
27
98542
5731
如果比較一次需要 1 秒,
那麼你需要花費 9.5 天才能完成工作。
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
這種方法總共需要比較
409,280 次,
02:32
would be 409,280,
42
152123
3860
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 本書,
這樣大約需要七輪分割,
也就是 8960 秒。
03:41
or 8,960 seconds.
63
221256
2691
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 視頻。 您將看到來自世界各地的一流教師教授的英語課程。 雙擊每個視頻頁面上顯示的英文字幕,從那裡播放視頻。 字幕與視頻播放同步滾動。 如果您有任何意見或要求,請使用此聯繫表與我們聯繫。