Workshop · floor model
Sorting is solved. Choosing how to sort is not.
Sorting looks like the most settled problem in computing. Python has
sorted(), it works, and most of the time that is the end of it.
Underneath that one call sits a library choice, and the choice comes with
conditions attached.
This page runs Python in your browser so you can watch those conditions decide the outcome. Every figure below is measured on your machine while you wait. Where a number came from research instead, it says so.
Sorting is worth the tour even if you never think about it, because the questions it asks come back at every scale. How much do you keep. How much do you throw away. What happens when work arrives faster than you can deal with it. Those are sorting questions at ten thousand items and infrastructure questions at ten million, and learning to read them in a small case is how you start recognising them in a large one.
Read the findings, run them yourself. Then switch to Play and see whether they stuck.
Ten rows, kept in this browser. An arcade cabinet had a few bytes of NVRAM and stored exactly this — not the plays, just the table. That is the same shape as round 2 below: an unbounded stream in, a bounded top-N kept. No server, nothing about you leaves this tab.
A leaderboard has to stay in order while scores arrive one at a time. The obvious code appends the new score and sorts the list again.
The alternative never sorts. It finds where the score belongs and inserts it there, so the list is ordered the whole way through.
Both are correct. One does O(n² log n) work and the other
O(n log n), so the gap widens as the data grows. That is why this
passes code review on a small test and fails in production.
This one runs in JavaScript, so it starts instantly and downloads nothing. It is a fact about algorithms rather than about a language, so it holds anywhere.
| Scores added | Re-sort each time | Keep it sorted | Ratio |
|---|
The full version of this question — in Python, with four alternatives and your choice of size — is its own floor model: you want this sorted how?
The survey is about Python libraries, and no Python has run yet. This is real
CPython in your browser — actual sorted(), numpy and
sortedcontainers, compiled to WebAssembly.
Two comparisons run. The first sorts the same numbers two ways,
sorted() against numpy.sort, which asks whether the
library matters even when the algorithm is settled. The second repeats the
leaderboard test above with SortedList doing the work, so you can
watch that result hold in the language it was measured in.
It downloads 10–20 MB the first time, so it waits until you ask for it.
| What ran | First | Second | Ratio here |
|---|
sorted() at roughly 30 seconds against about 2 seconds for
polars, a ~15× gap. polars runs in this browser runtime; 100 million rows do
not fit in one tab's memory budget.
None of the above is your situation. You have a real one: a particular language, a particular shape of data, a particular thing that must not break.
Describe it below. The prompt assembles as you pick, and you take it to whichever assistant you already use. It asks for the conditions rather than a verdict, which is the habit worth keeping.
Assembled in this tab. Nothing is sent anywhere, and the page keeps no record of what you picked. The prompt asks your assistant for the conditions under which each option wins, and to say plainly when it is guessing.
These libraries do different kinds of sorting, so picking one is usually about a capability rather than a benchmark: whether the order has to hold between inserts, whether you want the permutation rather than the values, whether the data fits in memory at all.
sorted() | Stable. Sorts anything comparable. key= takes any function. The right default, and the only one that needs no dependency. |
numpy.sort | kind= picks the algorithm — quicksort, mergesort, heapsort, stable. argsort returns the permutation instead of the data, which is what you want when sorting one array by another. |
SortedContainers | Not a faster sort — it never sorts. The collection is ordered at all times, so inserts are O(log n) and you get bisect and index access for free. |
polars | Multi-column, lazy, and out-of-core. Sorts data larger than memory, which the others simply cannot do. |
| radix / counting | Non-comparison sorts: O(n) for bounded integers, beating the O(n log n) floor by not comparing at all. Narrow, and unbeatable where they apply. |
None of this says stop using sorted(). It says the default comes
with conditions.
Knowing the category also does something beyond helping you pick. You cannot consider an architecture you do not know exists, so reading widely is how an option becomes available to you at all.
A survey gives you the conditions under which each option wins, rather than a winner. That is the difference between shopping at the hardware store and being able to work there.
Field Notes — the surveys stay neutral; the Reports built from them argue a case, and a new one is at the center of each edition.
No tracking pixels. Unsubscribe in one click.