01Chapter 1: What a Comparison Sort DoesA sorting algorithm puts a sequence of elements into order. Most general-purpose sorts in standard libraries are comparison sorts. A comparison sort learns about the data only by asking one question about two elements at a time: "should a come before b?" This chapter explains what that restr2 min read
02Chapter 2: Keys, Comparators and Equal ElementsThis chapter assumes you can write a small Python program, pass a function as an argument, and read Big-O notation. It covers the two ways you tell a sort what "smaller" means, and what it means for two elements to count as "equal" when the sort sees them.3 min read
03Chapter 3: What Makes a Sort StableThis chapter assumes you can read basic Python, know what a list of tuples or records is, and have seen a key function passed to a sort. The previous chapter showed that a comparator can call two different elements "equal" when their keys match. This chapter covers what a sort does with those equal 3 min read
04Chapter 4: When Stability Changes the ResultThis chapter assumes you can read basic Python and that you already know the definition from the previous chapter: a stable sort keeps elements with equal keys in their original input order. The question here is practical. When does that guarantee change what your program outputs, and when does it m2 min read
05Chapter 5: Sorting by Several KeysThis chapter assumes you already know what a stable sort is and how key functions and comparators work. Both were covered in earlier chapters. The examples use Python and Java, but the same ideas apply in any language.2 min read
06Chapter 6: Which Classic Algorithms Are Stable, and WhyThis chapter assumes you can read Python and already know what a stable sort is. A stable sort keeps elements with equal keys in their original input order. Here we look at how each classic algorithm moves elements, because that movement decides whether it is stable.2 min read
07Chapter 7: Getting Stable Results from an Unstable SortThis chapter assumes you know what a stable sort is, that you can write a comparator or key function, and that you know quicksort and heapsort are unstable. The examples use C++, but the technique works in any language.2 min read
08Chapter 8: Writing Comparators the Library Can TrustThis chapter assumes you can pass a comparator or key function to a sort, and that you know how equal keys behave under a stable sort. A sort routine treats your comparator as ground truth. If the comparator breaks the rules the library relies on, the result is not a slightly wrong order. You can ge3 min read
09Chapter 9: Timsort: Runs, Merges and GallopingThis chapter assumes you know how merge sort and insertion sort work, what a stable sort is, and how to read Big-O notation. Timsort is the stable sort behind Python's list.sort and sorted, Java's sort for object arrays, and V8's Array.prototype.sort. Tim Peters wrote it for Python in 2002. It3 min read
10Chapter 10: Introsort: Quicksort with a Safety NetThis chapter assumes you know how quicksort partitions around a pivot, what heapsort and insertion sort do, and Big-O notation. It also builds on the earlier chapter on which classic algorithms are stable.2 min read
11Chapter 11: Pattern-Defeating QuicksortThis chapter assumes you have read the introsort chapter. You should know how quicksort partitions around a pivot, why a bad pivot leads to O(n²) time, and how introsort falls back to heapsort. Pattern-defeating quicksort (pdqsort) was introduced by Orson Peters in 2016. It keeps introsort's worst-c2 min read
12Chapter 12: How Python, Java, C++, JavaScript, Rust and Go SortThis chapter assumes you know what a stable sort is, how multi-key sorting depends on it, and the basic ideas behind Timsort, introsort and pattern-defeating quicksort (pdqsort) from the previous chapters. It also assumes you can read short programs in several languages.2 min read
13Chapter 13: What Stability CostsThis chapter assumes you know Big-O notation for time and space, the stability of merge sort and quicksort, and the library functions covered in the previous chapter. Stability is a guarantee, and libraries pay for it in memory, speed, or both. This chapter shows where that cost comes from and when 3 min read
14Chapter 14: Testing Whether a Sort Is StableThis chapter assumes you know the definition of a stable sort, can write a key function, and can read Python. Its question is practical: given a sort function you didn't write, how do you find out whether it keeps equal elements in their input order? The documentation may be silent, out of date, or 2 min read