What's the fastest way to alphabetize your bookshelf? - Chand John

3,894,455 views ・ 2016-11-28

TED-Ed


Please double-click on the English subtitles below to play the video.

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
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
01:29
then 1,278, and so on,
25
89331
4292
01:33
adding up to 818,560 comparisons.
26
93623
4919
01:38
If each took just one second, the process would take over nine days.
27
98542
5731
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
02:35
taking almost five days.
43
155983
2152
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
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
03:41
or 8,960 seconds.
63
221256
2691
03:43
Sorting these sub-lines would add about 22 seconds each.
64
223947
4789
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
About this website

This site will introduce you to YouTube videos that are useful for learning English. You will see English lessons taught by top-notch teachers from around the world. Double-click on the English subtitles displayed on each video page to play the video from there. The subtitles scroll in sync with the video playback. If you have any comments or requests, please contact us using this contact form.

https://forms.gle/WvT1wiN1qDtmnspy7