liveBook · Manning

Quadratic time complexity

As explained in Grokking Algorithms, Second Edition

O(n2) is a quadratic time-complexity bound whose operation count grows with the square of the input size.

Quadratic time complexity, written O(n²), means an algorithm’s operation count grows with the square of its input size. It can become slow as inputs grow, so compare growth in operations rather than elapsed seconds. section 1.10

A repeated scan is a common path to quadratic time:

  • Selection sort finds the next highest-played artist by scanning the list, then repeats the scan. Doing an O(n) scan n times gives O(n²). section 2.9
  • Quicksort takes O(n²) in the worst case when its call stack has O(n) levels and each level touches O(n) elements. section 4.5

Selection sort checks fewer elements on later scans, but its total work is still O(n²): the average scan is about half the list, and Big O ignores that constant factor. Quicksort can also have quadratic worst-case time, even though its average time is O(n log n). section 2.9 section 4.3

How Grokking Algorithms, Second Edition covers it

How deep it goes
Works through repeated scans and quicksort’s worst case, while deferring a fuller discussion of Big O constants.
Examples
Uses artist ranking by play count and quicksort’s call-stack levels as examples.
What it assumes
Builds on the idea that an O(n) scan touches each list element once.
What you can do after
Evaluate algorithm growth by relating repeated scans or call-stack levels to operation counts.
How it connects
Connects Big O notation with selection sort, quicksort, and merge sort.

Also explained in

Compare how 4 books explain quadratic time complexity →

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.