Counting Sort

  • Explain and trace Counting sort on a given sequence of data.
  • Justify the time and space complexity of Counting sort in the worst case, based on the data size and data range.

Imagine we are sorting a collection of (small) integers where the range of the values is known.

Counting sort works by iterating through the input, counting the number of times each item occurs and using those counts to build the sorted array.

Demo
Analysis

Counting sort has a $\Omicron(k+n)$ running time where input contains $n$ integers in the range $[0, k)$.

  • $\Omicron(k)$ to create the frequency table (counts array).
  • $\Omicron(n)$ to put $n$ values into frequency table.
  • $\Omicron(k)$ to reproduce the sorted sequence.

Counting sort is efficient if the range of input data, $k$, is not significantly greater than the number of objects to be sorted, $n$.

If $k \in \Omicron(n)$ then counting sort runs in $\Omicron(n)$.

Counting sort takes $\Omicron(n + k)$ space:

  • $\Omicron(n)$ input space
  • $\Omicron(k)$ auxiliary space

Note that Counting sort only works when the range of potential items in the input is known ahead of time.

Resources