liveBook · Manning

O(n * log n)

As explained in Grokking Algorithms, Second Edition

O(n * log n) is a time-complexity bound whose operation count grows as the input size multiplied by its logarithm.

O(n * log n) describes an algorithm whose operation count grows with the input size multiplied by its logarithm. It grows faster than O(log n) or O(n), but much slower than O(n²) as inputs become large. section 1.10 section 4.3

Use these familiar algorithms to place the bound on the performance scale:

  • Merge sort runs in O(n log n) every time. section 4.3
  • Quicksort averages O(n log n) when its pivots divide the array reasonably, but can reach O(n²) in the worst case. section 4.5
  • Selection sort is O(n²), making it slower for large inputs than an O(n log n) sorting algorithm. section 4.3

Why quicksort can take O(n log n)

At one level of quicksort, partitioning touches all n elements, so that level costs O(n). If the partitions stay balanced, the call stack has O(log n) levels. Multiplying the work per level by the number of levels gives O(n) * O(log n) = O(n log n). Random pivot selection produces this running time on average, except for cases such as an array containing only identical elements. section 4.5

Estimates based on a slow computer that performs 10 operations per second

Use the run-time estimates to compare how O(n log n) grows against logarithmic, linear, quadratic, and factorial bounds. section 4.3

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains the bound through per-level work and call-stack height, then distinguishes guaranteed, average, and worst-case behavior.
Examples
Uses merge sort, quicksort, selection sort, estimated growth charts, and balanced partition levels as concrete examples.
What it assumes
Assumes basic Big O notation, arrays, recursion, partitioning, and call-stack levels.
What you can do after
Evaluate sorting algorithms by recognizing O(n log n) growth and separating average-case from worst-case performance.
How it connects
Connects Big O notation with quicksort, merge sort, selection sort, divide and conquer, and call-stack height.

Also explained in

Compare how 2 books explain O(n * log n) →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.