Is sorting faster than O(n)?
No, not in general. Any sort has to read each of its items at least once, so linear time, O(n), is the floor for sorting n items. Some methods reach O(n) on special kinds of data, such as whole numbers in a small, known range. For general comparison-based sorts, O(n log n) is the best possible worst case.
Why linear time is the floor
Big O describes how running time grows as the input size, n, grows. An algorithm that must look at every item takes at least a number of steps proportional to n.
Sorting has to examine every item. If an algorithm skipped one item, that item could hold any value, and the algorithm would have no way to know where it belongs. Checking whether the input is already sorted also reads every item. So no correct sorting algorithm can finish in less than about n steps in the general case.
Comparison sorts can’t beat n log n
Most general-purpose sorts work by comparing pairs of items. Each comparison answers one question: is this item smaller than that one? For n distinct items there are n! possible orders, and the algorithm has to end up knowing which order it is dealing with. A single comparison can split the remaining possibilities into two groups at most. So the number of comparisons needed in the worst case is at least log₂(n!), and that value grows roughly like n log n.
This is a bound on the worst case. The heapsort algorithm and merge sort both reach O(n log n) in the worst case, so no comparison sort can do better than that in the worst case. Quicksort’s average time is also O(n log n).
Linear-time sorts that don’t compare items
Some methods sort without comparing items against each other. They use the key values directly to decide where each item goes. That is how they can beat the n log n bound, but they depend on assumptions about the data.
Counting sort
Counting sort works when the keys are whole numbers in a known, limited range from 0 to k. It counts how often each key appears, turns those counts into starting positions, and places each item in its slot. Its running time is O(n + k).
For example, sorting a million ages recorded as whole numbers from 0 to 120 needs a few passes over the million items and 121 counters. Here k is small, so the time grows with n. If the keys were 64-bit integers, the counters alone would be impractical.
Radix sort
Radix sort handles integers or fixed-length strings one digit, or one character position, at a time. It uses a stable sort on each position, such as counting sort. With d digit positions and base b, the running time is O(d(n + b)). When the key length and base are fixed, that is linear in n.
Radix sort works from the least significant position to the most significant. Each pass has to keep the order set by the earlier passes, which is why stability is required for the result to be correct.
Bucket sort
Bucket sort places items into buckets by value, sorts each bucket, and joins the buckets in order. If the values are spread roughly evenly, each bucket holds only a few items, and the expected running time is O(n). If most items land in one bucket, the time is close to the cost of the sort used inside that bucket, which can be much slower.
The assumptions cost something
These methods depend on the data. Counting sort needs a small key range. Radix sort needs keys of a fixed length. Bucket sort needs values that are spread out. Real data often breaks these assumptions, with floating-point numbers, long strings, and skewed values being common examples. In those cases, a comparison sort such as merge sort or Timsort is the usual choice, and its O(n log n) bound is the number to plan around.
Are the linear-time sorts stable?
Counting sort and radix sort are usually stable when written the standard way. Bucket sort’s stability depends on the sort used inside each bucket. If you’re asking which sorting algorithms are stable, the answer for these methods is usually yes, and radix sort relies on that stability to produce correct output.
Key takeaways
- No sort is faster than O(n) in general, because every item has to be read.
- Comparison sorts are bounded below by O(n log n) in the worst case.
- Counting, radix, and bucket sorts can reach O(n), or expected O(n), when the data fits their assumptions.
- No single method is fastest for every input, so match the sort to your data.