Whats the fastest way to alphabetize your bookshelf Chand John

You work at the college library.

You’re in the middle of a quiet afternoon

when suddenly a shipment of 1,280
different books arrives.

The books have been dropped of
in one long straight line,

but they’re all out of order,

and the automatic sorting
system is broken.

To make matters worse,
classes start tomorrow,

which means that
first thing in the morning,

students will show up in droves
looking for these books.

How can you get them all sorted in time?

One way would be to start at one end
of the line with the first pair of books.

If the first two books are in order,
then leave them as they are.

Otherwise, swap them.

Then, look at the second and third books,

repeat the process,

and continue until you reach
the end of the line.

At some point, you’ll come across
the book that should be last,

and keep swapping it with every
subsequent book,

moving it down the line until it
reaches the end where it belongs.

Then, start from the beginning
and repeat the process

to get the second to last book
in its proper place,

and keep going until all books are sorted.

This approach is called Bubble Sort.

It’s simple but slow.

You’d make 1,279 comparisons
in the first round,

then 1,278, and so on,

adding up to 818,560 comparisons.

If each took just one second,
the process would take over nine days.

A second strategy would be to start
by sorting just the first two books.

Then, take the third book and compare it
with the book in the second spot.

If it belongs before the second book,
swap them,

then compare it
with the book in the first spot,

and swap again if needed.

Now you’ve sorted the first three books.

Keep adding one book at a time
to the sorted sub-line,

comparing and swapping the new book
with the one before it

until it’s correctly placed
among the books sorted so far.

This is called Insertion Sort.

Unlike Bubble Sort, it usually doesn’t
require comparing every pair of books.

On average, we’d expect to only need
to compare each book

to half of the books that came before it.

In that case, the total
number of comparisons

would be 409,280,

taking almost five days.

You’re still doing
way too many comparisons.

Here’s a better idea.

First, pick a random book.

Call it the partition and compare it
to every other book.

Then, divide the line

by placing all the books
that come before the partition on its left

and all the ones that come after it
on its right.

You’ve just saved loads of time

by not having to compare
any of the books on the left

to any of the ones
on the right ever again.

Now, looking only
at the books on the left,

you can again pick a random partition book

and separate those books that come
before it from those that come after it.

You can keep creating
sub-partitions like this

until you have a bunch of small sub-lines,

each of which you’d sort quickly using
another strategy, like Insertion Sort.

Each round of partitioning requires
about 1,280 comparisons.

If your partitions are pretty balanced,

dividing the books into 128 sub-lines of ten
would take about seven rounds,

or 8,960 seconds.

Sorting these sub-lines would add
about 22 seconds each.

All in all, this method
known as QuickSort

could sort the books
in under three and a half hours.

But there’s a catch.

Your partitions could end up lopsided,
saving no time at all.

Luckily, this rarely happens.

That’s why QuickSort is one of the most
efficient strategies

used by programmers today.

They use it for things like sorting items
in an online store by price,

or creating a list of all the gas stations
close to a given location

sorted by distance.

In your case, you’re done quick sorting
with time to spare.

Just another high-stakes day
in the library.

你在大学图书馆工作。

你正处于一个安静的下午

,突然一批 1,280
种不同的书籍到达。

书本是
一条很长的直线被扔掉了,

但都乱七八糟了

,自动分拣
系统坏了。

更糟糕的是,
明天开始上课,

这意味着
早上第一件事,

学生会成群结队地出现在
寻找这些书。

你怎样才能让它们及时排序?

一种方法是从
第一对书的一端开始。

如果前两本书按顺序排列,
则保持原样。

否则,交换它们。

然后,看第二和第三本书,

重复这个过程

,直到你到达
行尾。

在某些时候,您会遇到
应该放在最后的书,

并不断将其与
后续的每一本书交换,

将其向下移动,直到它
到达它所属的末尾。

然后,从头开始
并重复该过程

,将倒数第二本书
放在适当的位置,

并继续进行,直到所有书籍都排序完毕。

这种方法称为冒泡排序。

这很简单但很慢。

您将在第一轮进行 1,279 次比较

然后是 1,278 次,以此类推,

总计 818,560 次比较。

如果每个只需要一秒钟,
那么这个过程将需要九天以上。

第二种策略是从
仅排序前两本书开始。

然后,拿第三本书和
第二个地方的书比较。

如果它属于第二本书之前,则
交换它们,

然后将其
与第一本书的书进行比较,

如果需要再次交换。

现在您已经对前三本书进行了排序。

继续将一本书一次添加
到已排序的子行,

将新书与前一本书进行比较和交换

直到它正确放置
在到目前为止已排序的书籍中。

这称为插入排序。

与冒泡排序不同,它通常
不需要比较每一对书。

平均而言,我们预计
只需将每本书

与之前的一半书籍进行比较。

在这种情况下,
比较总数

将是 409,280 次,

需要将近五天时间。

你还在
做太多的比较。

这是一个更好的主意。

首先,随机挑选一本书。

将其称为分区并将其
与其他所有书籍进行比较。

然后,

通过将
分区之前的所有书籍放在其左侧,将分区之后的所有书籍放在右侧来划分线

无需再将
左侧的任何书籍与右侧的

任何书籍进行比较,从而节省了大量时间。

现在,
只看左边的书,

你可以再次随机选择一本分区

书,并将
之前的书与之后的书分开。

您可以
像这样继续创建子分区,

直到您有一堆小的子行

,您可以使用
另一种策略(例如插入排序)快速对每个子行进行排序。

每一轮分区需要
大约 1,280 次比较。

如果您的分区相当平衡,

将书籍分成 128 个子行,每行 10
个,大约需要 7 轮,

即 8,960 秒。

对这些子行进行排序将增加
大约 22 秒。

总而言之,这种
称为 QuickSort 的方法

可以
在三个半小时内对书籍进行分类。

但有一个问题。

您的分区最终可能会倾斜,
根本不会节省时间。

幸运的是,这种情况很少发生。

这就是为什么 QuickSort 是当今程序员使用的最
有效的策略

之一。

他们将其用于诸如
按价格对在线商店中的商品进行分类,

或创建按距离排序的靠近给定位置的所有加油站的列表

在你的情况下,你已经完成了快速排序,
还有时间。

图书馆又是一个高风险的
日子。