Du arbeitest in der Universitätsbibliothek. Es ist ein ruhiger Nachmittag, als plötzlich eine Lieferung von 1280 unterschiedlichen Bücher ankommt. Die Bücher wurden in einer lange, geraden Reihe aufgestellt, doch sie sind nicht geordnet, und das automatische Sortiersystem ist kaputt. Zu allem Übel fängt morgen wieder der Unterricht an, was bedeutet, dass gleich morgen früh Studenten scharenweise nach diesen Büchern suchen werden. Wie schaffst du es alle rechtzeitig zu ordnen? Du könntest z. B. an einem Ende der Reihe mit dem ersten Buchpaar anfangen. Wenn die ersten zwei Bücher geordnet sind, dann lass sie so. Ansonsten, tausche sie. Sieh dir dann das 2. und 3. Buch an, wiederhole den Vorgang, und mach so weiter bis zum Ende der Reihe. Irgendwann stößt du auf das Buch, das als letztes stehen sollte. Du tauscht es weiterhin mit den darauffolgenden Büchern, und rückst es somit an das Ende der Reihe, wo es hingehört. Dann beginnst du wieder von vorne und wiederholst den Vorgang, um das zweitletzte Buch an seinen Platz zu bringen. So machst du weiter, bis alle Bücher sortiert sind. Diese Methode heißt "Bubblesort". Sie ist sehr einfach, aber langsam. Du würdest 1279 Vergleiche in der erste Runde machen, dann 1278, und so weiter. Insgesamt wären das 818 560 Vergleiche. Würde jeder Vergleich nur eine Sekunde benötigen, dann würde das Verfahren mehr als neun Tage dauern. Eine zweite Strategie wäre, zuerst nur die ersten beiden Bücher zu ordnen. Nimm dann das dritte Buch und vergleiche es mit dem Buch an zweiter Stelle. Gehört es vor's zweite Buch, tausche sie. Vergleiche es dann mit dem Buch an erster Stelle, und tausche die beiden, wenn nötig. Jetzt hast du die ersten drei Bücher sortiert. Füge ein Buch nach dem anderen zu der geordneten Bücherreihe hinzu, indem du es mit den Büchern davor vergleichst und tauschst, bis es an der richtigen Stelle unter den schon sortierten Büchern steht. Dieses Verfahren heißt "Insertionsort". Anders als bei Bubblesort, muss man nicht jedes Buchpaar miteinander vergleichen. Im Durchschnitt erwarten wir, dass wir jedes Buch nur mit der Hälfte der Bücher, die davor kommen, vergleichen müssen. In diesem Fall liegt die Gesamtzahl der Vergleiche bei 409 280, und dauert fast fünf Tage. Du machst immer noch viel zu viele Vergleiche. Hier eine bessere Idee: Zuerst wählst du willkürlich ein Buch aus. Das ist das Trennelement, das du mit jedem anderen Buch vergleichst. Teile dann die Reihe, indem du alle Bücher, die vor dem Trennelement stehen, links davon anordnest und alle die danach kommen, rechts davon. Du hast gerade sehr viel Zeit gespart, denn du musst nie wieder die Bücher auf der linken Seite mit den Büchern auf der rechten Seite vergleichen. Betrachte jetzt nur die Bücher auf der linken Seite. Du kannst hier wieder ein Buch als Trennelement wählen, und die Bücher, die davor und danach kommen, voneinander trennen. So kannst du immer weitere Unterteilungen erzeugen, bis du eine Menge kleiner Teilbereiche hast, die du durch eine andere Sortiermethode, wie Insertionsort, schnell ordnen kannst. Bei jeder Unterteilung fallen etwa 1280 Vergleiche an. Wenn deine Aufteilungen relativ gleichmäßig sind, brauchst du, wenn du die Bücher in 128 Teilbereiche à zehn Bücher teilst, ca. 7 Runden, oder 8960 Sekunden. Das Sortieren dieser Teilbereiche braucht jeweils 22 Sekunden. Alles in allem könntest du mit dieser Methode namens "Quicksort" die Bücher in unter dreieinhalb Stunden ordnen. Doch das hat einen Haken: Sind die Unterteilungen ungleichmäßig, sparst du keine Zeit. Glücklicherweise passiert das selten. Deshalb ist Quicksort eine der effizientesten Strategien, die heute von Programmierern benutzt wird. Damit werden in Onlineshops die Waren nach dem Preis geordnet, oder eine Liste aller Tankstellen in der Nähe eines gegeben Ortes nach Entfernung sortiert. In deinem Fall bist du mit dem Ordnen fertig, und hast noch Zeit über. Wieder ein anspruchsvoller Tag in der Bibliothek.
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.