5-min deep dive

Sorting in depth: complexity, stable sorts and binary search

When choosing a sorting method, look at “how the effort grows as the data grows” and “what kind of ordering it handles badly.” The yardstick for that is big-O notation.

Reading time: about 5 min

← Start with the 1-min version

Big-O describes “how it grows”

O(n²) means “if the number of items n doubles, the effort roughly quadruples”; O(n log n) means “it grows only a little more than double.” Ignore the fine constants and look only at how it grows.

Number of items nn²n log₂ n
1,0001 millionabout 10,000
1 million1 trillionabout 20 million

Even on a computer that can make 100 million comparisons per second, sorting 1 million items at O(n²) takes about 3 hours; at O(n log n), about 0.2 seconds. The more data, the wider the gap between methods — by orders of magnitude.

Strengths and weaknesses of four sorts

SortAverageWorstNotes
Selection sortO(n²)O(n²)Finds the minimum of the rest and moves it forward. Always n(n−1)÷2 comparisons
Insertion sortO(n²)O(n²)Very fast on almost-sorted data
QuicksortO(n log n)O(n²)Among the fastest on average. Slow if pivots are chosen badly
Merge sortO(n log n)O(n log n)Always fast, but needs extra working memory

Quicksort keeps splitting data into a “smaller set” and a “larger set” around a pivot value. If the pivot is always the smallest value, only one item goes to one side, splitting takes nearly n rounds, and it slows all the way to O(n²).

What is a “stable sort”?

A sort that keeps the original order of items with equal values is called a stable sort. For example, if applications listed in order of arrival are re-sorted by score, people with the same score stay in arrival order.

Insertion sort and merge sort are stable; selection sort and quicksort (as usually written) are not. Python’s standard sort is a stable sort combining merge sort and insertion sort, so if you “sort by name first, then by score,” people with the same score end up in name order.

Sorted data is fast to search: binary search

With sorted data, you can compare with the middle item, decide “left half or right half,” and halve the candidates each time. This is binary search: it finds an item among 1,000 in about 10 comparisons, and among 1 million in about 20 (about log₂ n times). If the data isn’t sorted, you have no choice but to check one by one from the end (up to n times).

Field note: databases speed up with a “sorted index”

A database can find “customer number 123” among millions of records in an instant because it keeps an index — a sorted table of contents. Following the index gets there in few steps, like binary search. The trade-off: the index must be updated on every write, so adding too many indexes slows writes down.

Practice questions

Four-choice questions in the style of Japan’s Fundamental Information Technology Engineer Examination. Tap below to see the answer.

Q1How many comparisons does selection sort make to sort n items?

  • An
  • B2n
  • Cn(n−1)÷2
  • Dn²
Show the answer and explanation

Answer: CTo find the smallest of the rest each time, selection sort makes (n−1) + (n−2) + … + 1 = n(n−1)÷2 comparisons. It’s the same number regardless of how the data is ordered.

Q2When is quicksort’s processing time at its worst?

  • AWhen the number of items is even
  • BWhen the minimum (or maximum) value is chosen as the pivot every time
  • CWhen no two values are the same
  • DWhen all the data fits in memory
Show the answer and explanation

Answer: BIf only one item goes to one side of each split, splitting takes nearly n rounds, and it slows all the way to O(n²).

Q3You use binary search to find an item in 1,000 sorted records. Which is closest to the maximum number of comparisons?

  • A10
  • B100
  • C500
  • D1,000
Show the answer and explanation

Answer: ABinary search halves the candidates with each comparison. 2 to the 10th is 1,024, so among 1,000 items it finds the target in about 10 comparisons.

Summary
  • Big-O describes how effort grows as data grows
  • Quicksort is fastest on average but O(n²) at worst. Merge sort is always O(n log n) but uses memory
  • A stable sort keeps the order of equal values. Sorted data can be binary-searched in about log₂ n steps