liveBook · Manning

Performance

As explained in Grokking Algorithms, Second Edition

Big O notation is a mathematical notation for describing the asymptotic growth rate of an algorithm's resource usage as input size increases.

Suppose you need to search a long list. Checking entries one by one may feel quick when the list is short—but what happens when it grows to a billion entries? To compare algorithms at different sizes, count how their operations grow rather than timing one run in seconds. Big O notation names that growth rate. section 1.5 section 1.10

A small test can mislead you

If checking an entry takes 1 ms, simple search needs up to 100 ms for 100 entries; binary search needs about 7 ms. With a billion entries, the estimates become a billion milliseconds—about 11 days—for simple search and about 30 ms for binary search. The gap does not stay fixed as the list grows. section 1.7

Let n be the input size. Simple search is O(n): in the worst case, it checks every entry. Binary search is O(log n): its number of steps grows much more slowly. For a list of four billion items, their maximum counts are four billion checks and 32 guesses, respectively. section 1.5 section 1.9

Try it with paper

To make a grid of 16 boxes, drawing each box takes 16 operations, or O(n). Folding paper so each fold doubles the number of boxes takes four operations, or O(log n). The steps you choose change how the work grows. section 1.8

Common running times, from faster-growing work to slower-growing work as inputs increase, are:

A lucky search can finish on the first entry, but that does not change simple search’s O(n) worst-case label. Average-case time is worth considering too. Nor does a Big O label convert neatly into an exact operation count or number of seconds. section 1.9 section 1.10

The same idea applies beyond lists. Breadth-first search follows graph edges and queues people to search, giving O(V+E) time: V for vertices and E for edges. section 6.8

Run times grow at very different speeds!

Compare how the running times of simple search and binary search diverge as the list grows. section 1.7

How Grokking Algorithms, Second Edition covers it

How deep it goes
Works through operation counts and common Big O classes; flags exact time conversions as simplified and defers average-case discussion.
Examples
Uses a billion-entry search comparison, a folded-paper grid, a phone-book search, and graph traversal.
What it assumes
Starts with list searches and counting steps, introducing linear and logarithmic time through their contrasting guess counts.
What you can do after
Evaluate/understand how candidate algorithms scale and identify worst-case running times from their operations.
How it connects
Connects Big O notation to simple search, binary search, sorting algorithms, and breadth-first search.

Also explained in

Compare how 40 books explain performance →

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.