Big O Notation

O(n) vs O(n log n): Which Is Faster?

Compare the two growth rates with real numbers, see where the log factor hurts, and learn why sorting is benchmarked at n log n. O(n) vs O(n log n) explained.

On this page
  1. Short answer
  2. How big the log factor gets
  3. Examples of each
  4. Why sorting is benchmarked at n log n
  5. Speed is not the only trait
  6. Is Timsort faster than quicksort?
  7. Reading the comparison
  8. Summary

Short answer

O(n) grows more slowly than O(n log n), so for large inputs an O(n) algorithm is generally the faster one. The gap is smaller than the names suggest, though. The log factor grows slowly, and constant factors in real code can decide which algorithm is faster at small sizes.

How big the log factor gets

Here are approximate step counts, using log base 2. They ignore constant factors, so they show growth rather than running time.

n O(n) steps O(n log n) steps
10 10 about 33
1,000 1,000 about 10,000
1,000,000 1,000,000 about 20,000,000

At a million items, the n log n version does about 20 times the work of the linear one. That is a real gap, but both are far better than O(n²), which would take about a trillion steps at that size.

Examples of each

O(n) examples:

  • Finding the largest value in an unsorted list by checking each item once.
  • Adding up the values in a list.
  • Linear search through unsorted data.

O(n log n) examples:

  • Merge sort, which splits the input in half repeatedly and merges the results.
  • Heapsort, which uses a binary heap. The heapsort algorithm runs in O(n log n) time in its best, average and worst cases.
  • Many divide-and-conquer methods, where the log n comes from repeated halving.

Why sorting is benchmarked at n log n

Comparison-based sorting cannot do better than about n log n comparisons in the worst case. That is why O(n log n) is the usual benchmark for sorting, and why many people ask whether sorting can be done in O(n).

Sorting can take O(n) time for some special inputs, such as whole numbers in a limited range, using methods like counting sort or radix sort. Those methods make assumptions about the keys, so they are not general replacements for comparison sorts.

Speed is not the only trait

Two sorts with the same Big O can behave differently. Stability, meaning whether a sort keeps equal items in their original order, matters for tasks such as sorting records by one field after another. Knowing which sorting algorithms are stable helps you choose when that property matters.

Is Timsort faster than quicksort?

Readers often ask “Is Timsort faster than quicksort?” Both are O(n log n) on average. Timsort is O(n log n) in the worst case and is designed to take advantage of runs of data that are already partly ordered. Quicksort can drop to O(n²) when its pivots are poor. Neither wins for every input, so the answer depends on what you are sorting.

Reading the comparison

Big O tells you which algorithm grows more slowly, not which one finishes first on your computer at your input size. For small inputs, a simpler algorithm with faster growth can win. If the choice matters, measure both with data that looks like yours.

Summary

For large inputs, O(n) has the edge in growth. The log factor in O(n log n) is modest but real, and the practical winner depends on constant factors, input size and whether the output has to be ordered.

The complete guide to big o notation

Big O for Data Structures
Read the guide

Keep exploring

Guides from other topics

All topics →