Skip to content

Sorting limits and counting sort

Show with a decision tree that any sort that only compares items needs at least log₂(n!), which is Ω(n log n), comparisons, and sort small integer keys in Θ(n + k) with counting sort.

It opens on

Three different numbers x, y, z are sorted by yes or no questions that compare two of them. Can some method always finish in 2 questions, or does some order of them need 3?

Why it matters

Merge sort and quicksort both stop at n log n, and the decision tree explains why no cleverer comparison sort will beat it; counting sort then shows what a sort can do when it stops comparing, and when that helps.

Good to know first

  • Insertion sort: it compares a[j] > key and shifts, and does 2 to 3 comparisons on 3 items
  • Merge sort: n log₂ n − n + 1 comparisons at worst, Θ(n log n)
  • Quicksort: Θ(n log n) on average and Θ(n²) at worst
  • Backtracking: n items can be put in n! orders, 3! = 6
  • Big-O, Big-Omega and Big-Theta: Ω(f) is a lower bound on the cost

Step by step

  1. A decision tree for three items

    Draw the tree of comparisons of insertion sort on three items, count its leaves, and show that no comparison sort can always finish in 2 comparisons.

    If x > z is yes, then z is compared with y, and its answer gives y, z, x or z, y, x, after 3 comparisons. The other side is the same, so every run of the sort on three items ends at one of six leaves, each a different order, two of them 2 comparisons down and four of them 3 down.

    the whole tree

    A common mix-up

    A cleverer method could sort any three items in 2 comparisons.

    In fact: Each comparison has two outcomes, so 2 comparisons separate at most 4 cases, and there are 6 orders.

  2. The bound for n items

    Show that any comparison sort needs at least log₂(n!) comparisons, that this is Ω(n log n), and compare the bound with merge sort's and insertion sort's counts.

    I ran merge sort and insertion sort on every order of 3, 4, 5 and 8 distinct items and kept the largest count. For 8 items log₂ 40320 is about 15.30, so at least 16 comparisons are needed, and merge sort's largest count was 17, one more. Insertion sort's was 28. For 5 items the least is 7 and merge sort needed 8.

    the table with the least and the two sorts

    A common mix-up

    Every list of n items needs n log n comparisons.

    In fact: The bound is for the worst input; insertion sort makes n − 1 comparisons on a sorted list, because that input ends at a shallow leaf.

  3. Counting sort

    Trace counting sort on small integer keys, show that it takes Θ(n + k) time and space, and say when that beats a comparison sort.

    So count is 1, 2, 0, 3, 2. The second loop adds each count to the one before, so count[v] becomes the number of items with a key of at most v, here 1, 3, 3, 6, 8.

    the counts

    A common mix-up

    Counting sort proves that Ω(n log n) is wrong.

    In fact: The bound is for sorts that learn only by comparing, and counting sort reads the key as an index, which needs small integer keys and Θ(k) space.

Try it

How many leaves must a decision tree have to sort 2 items, and how many comparisons is that at least?Answer

2 leaves and 1 comparison

Hint: 2! orders, and 2¹ = 2

How many comparisons does any comparison sort need at least on some input of 6 items?Answer

10

Hint: 6! = 720, 2⁹ = 512 is too few, 2¹⁰ = 1024 is enough

Counting sort with k = 3 on 2, 0, 3, 2, 1. What are the counts and the running totals?Answer

Counts 1, 1, 2, 1 and totals 1, 2, 4, 5

Hint: 2 appears twice, and the totals add the counts up to each key

Suppose x > y is yes, so the front is y, x, and z is compared with x. If x > z is no, what order has been found, and how many comparisons did it take?Answer

y, x, z, after 2 comparisons.

A comparison sort works on 4 items. How many leaves must its tree have, and what is the least number of comparisons it needs on some input?Answer

24 leaves, and 5 comparisons, since 2⁴ = 16 is too few and 2⁵ = 32 is enough.

Sorting limits and counting sort

30 minutes every month. No card.