liveBook · Manning

Linear time complexity

As explained in Grokking Algorithms, Second Edition

Linear time describes an algorithmic running time that grows proportionally with the size of the input.

Linear time describes an algorithm whose running time grows with the size of its input. If a simple search checks items one by one, a list of 100 items can require up to 100 guesses, while 4 billion items can require up to 4 billion guesses. The number of operations grows in step with the list. section 1.5

Big O notation writes linear time as O(n), where n represents the input size. This notation focuses on how operation counts grow rather than promising an exact number of seconds. section 1.10 section 1.12

Drawing a grid

Imagine drawing one box at a time. Drawing 16 boxes takes 16 steps, so the algorithm takes O(n) time. A folding method creates many boxes per operation and takes O(log n) time instead, showing why input growth matters. section 1.8

Use these comparisons to recognize linear growth:

  • Simple search may inspect every item, making its maximum number of guesses equal to the list size. section 1.5
  • Binary search grows logarithmically, so it becomes much faster than linear search as the list grows. section 1.12
  • O(n) can also describe a worst-case call-stack size, even when the full algorithm has a different running time. section 4.5
Drawing a grid one box at a time

Compare the visual sequence of drawing a grid one box at a time with faster ways to increase the number of boxes. section 1.8

How Grokking Algorithms, Second Edition covers it

How deep it goes
Defines O(n) through search and grid examples, contrasts O(log n), and stops short of exact operation-count guarantees.
Examples
Uses one-by-one phone-list searching, drawing a grid box by box, and quicksort’s worst-case call stack.
What it assumes
Assumes only familiarity with lists and operations while introducing running time and Big O through concrete examples.
What you can do after
Evaluate/understand whether an algorithm’s running time grows linearly with input size and compare it with logarithmic time.
How it connects
Connects linear time with Big O notation, simple search, binary search, logarithmic time, and quicksort.

Also explained in

Compare how 8 books explain linear 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.