An elegant coding principle can be used to alphabetize your bookshelf with lightning speed

Divided they fall.
Divided they fall.
Image: Unsplash/Annie Spratt
We may earn a commission from links on this page.

It’s easy to forget that the seemingly mundane sorting of goods and services online—like the map app that finds your nearest pizza joint, or the online retailer with the cheapest pair of running shoes—is powered by sophisticated computer algorithms. But the simple principles that power these sorting tools don’t just apply to online shopping—they offer organizational insights for everyday life.

Quicksort, one such sorting algorithm invented by a British computer scientist in the 1960s and considered one the most important British innovations of the 20th century, is still used today by coders to arrange large sets of data. Its methodology can help sort out our piles of material things, including the scattershot collections of printed books we all tend to hoard.

In a recently published TED-Ed video, software engineer and educator Chand John applies quicksort in this way, mapping out what to do with a hypothetical set of 1,280 books dumped on a student librarian in a college library to be alphabetized.

It begins with choosing a “partition” that should fall somewhere near the middle of your book titles lined up randomly in a row. In the TED-Ed video, for example, a book that would be filed under “N” is selected.

The surrounding books are then moved left or right of the partition depending on where they belong, which results in two subsets of books. The process is repeated, by choosing a partition between A and M, and another between N and Z, until there are four groups of books which are relatively close to each other in alphabetical order. The process runs until the groupings are small enough to easily sort the books individually.

According to Chand, using quicksort instead of other sorting methods would give you back days of your life otherwise spent sorting. In his analogy, Chand compares quicksort’s efficiency with the “bubble sort” and “insertion sort” methods, which are taught in beginner computer science classes. Bubble sorting, as applied to books, would involve systematically comparing one pair of books at a time and always moving the book that belongs last to the back spot, if it isn’t already there. After moving through your bookshelf this way—pair by pair—you’d discover which book belongs at the very end, and then go back to the beginning to repeat the process.

The insertion method, which is how most people naturally sort books, would have you compare the first two books on your shelf and swap them if necessary, then compare the third book to the second book and swap again. This difference here is that you’re arranging the books correctly as you move down the line.

Chand estimates that sorting 1,280 books that arrived in a straight line using the bubble method would take a person nine days, if each comparison took one second. Using the insertion method would add up to nearly five days of sorting. Using quicksort—with seven rounds of dividing and perfectly balanced partition sections—would get the job done in 3.3 hours, he says.

That would free up at least a week’s worth of reading that most of us wish we could do, but rarely ever pull off.