5/41
Part 1 of 7 · SortingLearn and do

What a sort costs

Every sort compares items, and counting the comparisons shows what it costs.

Python's sort is Timsort. It finds stretches that are already in order, called runs, and merges them. Here are its comparisons, counted with a class whose __lt__ adds one per call:

Predict

Timsort counted 999 comparisons on 1,000 items that were already sorted. About how many on 1,000 in random order?

Pick an answer above to see the rest