When to Use Non-Comparison Sorts

Conditions for counting sort and radix sort.

Non-comparison sorts beat O(nlogn)O(n \log n) but only under specific conditions.

Use counting sort when:

  • Values are integers in a small range [0,k][0, k] where k=O(n)k = O(n).
  • Example: Sorting exam scores from 00 to 100100 for n=1000n = 1000 students.

Use radix sort when:

  • Values are integers or strings with bounded length.
  • The number of digits dd is small compared to logn\log n.
  • Example: Sorting nn integers up to n2n^2. Radix sort takes O(n)O(n) because d=2d = 2 in base nn.

Avoid them when:

  • Range is huge (counting sort needs O(k)O(k) space).
  • Values are floats or arbitrary objects.
  • You need in-place sorting with O(1)O(1) extra space.