01Chapter 1: What a Comparison Sort DoesEvery sort function in a standard library answers one question: given a list of items, in what order should they come out? Most of them answer it the same way. They compare pairs of items and move items based on the results. This chapter explains that model, what it costs, and where it stops working7 min read
02Chapter 2: Keys, Comparators and Equal ElementsThis chapter assumes you can write a lambda or pass a function as an argument, and that you know what a record or struct is. It covers how a sort decides that one element comes before another, and why two elements can count as "equal" to the sort while still being different records. That gap between6 min read
03Chapter 3: What Makes a Sort StableThis chapter defines stability precisely and shows how to check whether a given sorted output is stable. It assumes you can write basic code in a mainstream language, know how lists and records work, can read Big-O notation, and have passed a key function or lambda to a sort before. The previous cha6 min read
04Chapter 4: When Stability Changes the ResultThis chapter assumes you can read Python, know what a key function is, and know the definition of a stable sort from the previous chapter: a stable sort keeps elements with equal keys in the order they had in the input. Here we look at when that guarantee changes what your program outputs, and when 6 min read
05Chapter 5: Sorting by Several KeysThis chapter assumes you can write a key function or comparator in Python or Java and that you know what a stable sort is. A stable sort keeps records with equal keys in their input order. Earlier chapters covered that definition. Here we use stability as a tool.6 min read
06Chapter 6: Which Classic Algorithms Are Stable, and WhyThis chapter assumes you already know what a stable sort is: it keeps elements with equal keys in their original relative order. It also assumes you can read Python and know Big-O notation. The goal is to explain why each classic algorithm is or is not stable, by looking at how it moves elements. 6 min read
07Chapter 7: Getting Stable Results from an Unstable SortThis chapter assumes you can read Python, C++, Rust and Go at a basic level. You should know what a comparator and a key function are, and what stability means: equal keys keep their input order. It also assumes you know that quicksort and heapsort are not stable.5 min read
08Chapter 8: Writing Comparators the Library Can TrustThis chapter assumes you can pass a lambda to a sort function and that you know the difference between a key function and a comparator from the chapter on keys and comparators. It also assumes you know what a stable sort promises. Here we look at the other side of that promise. A sort function can o6 min read
09Chapter 9: Timsort: Runs, Merges and GallopingThis chapter assumes you already know how merge sort and binary search work, and that you know the definition of a stable sort from earlier chapters. You should also be able to read Python that uses slices and the bisect module.6 min read
10Chapter 10: Introsort: Quicksort with a Safety NetThis chapter assumes you know how quicksort partitions an array, what a binary heap is, and why insertion sort is fast on small or nearly sorted inputs. The chapter on classic algorithms covered why quicksort and heapsort are unstable. Here you will see how introsort combines quicksort, heapsort and5 min read
11Chapter 11: Pattern-Defeating QuicksortThis chapter assumes you have read the introsort chapter. It also assumes you can follow recursive partition code and Big-O arguments, and you know that quicksort is unstable. Pattern-defeating quicksort, usually shortened to pdqsort, was published by Orson Peters in 2016. It keeps introsort's main 6 min read
12Chapter 12: How Python, Java, C++, JavaScript, Rust and Go SortThis chapter assumes you know what a stable sort is, how to sort by several keys, and how to break ties with the original index. Earlier chapters covered those topics. It also assumes you have a rough idea of how Timsort, introsort and pattern-defeating quicksort (pdqsort) work. Here we look at the 6 min read
13Chapter 13: What Stability CostsThis chapter assumes you have read the earlier chapters on what stability means, which classic algorithms are stable, and how Timsort, introsort and pattern-defeating quicksort (pdqsort) work. You should also be comfortable with Big-O notation for time and space.7 min read
14Chapter 14: Testing Whether a Sort Is StableThis chapter assumes you already know what a stable sort is, how key functions and comparators work, and why library sorts often switch to insertion sort for small inputs. The examples use Python, plus one C++ program. You should be comfortable writing small test functions and running them with `pyt5 min read