liveBook · Manning

Linear time complexity

As explained in Grokking Data Structures

Linear growth is growth proportional to input size.

Linear time means that an algorithm’s running time grows in proportion to its input size. Written as O(n), it describes growth that looks like a straight line when input size and running time are graphed. section 4.7 section 4.16

Use O(n) to reason about algorithms whose work scales with the number of input elements:

  • Scanning every element once requires linear time, even when the average search examines only about half the elements. section 4.14
  • Constant factors such as loop increments and comparisons do not change the linear classification, but calls inside the loop may add hidden costs. section 4.14
  • Running time and extra memory are separate measurements: an algorithm can take O(n) time while using O(1) extra space. section 4.14 section 4.12

Linear search

A linear search checks array elements in order until it finds the target or reaches the end. An unsuccessful search checks all n elements, so its running time is O(n). section 4.14

def linear_search(self, target):
    for i in range(self._size):        # repeat F(n) times
        if self._array[i] == target:       # cost: O(1)
            return i                       # cost: O(1)
        elif self._array[i] > target:      # cost: O(1)
            return None                    # cost: O(1)
    return None                        # cost: O(1)

The implementation makes the growth visible: the loop may repeat once for each element, while each comparison and return takes constant time. section 4.14

How Grokking Data Structures covers it

How deep it goes
Defines asymptotic linear growth and works through linear search, but stops short of formal proofs.
Examples
Uses linear search, array inversion, and dynamic-array queue resizing to illustrate linear work or storage.
What it assumes
Assumes only input size and basic algorithm operations, while introducing Big-O notation and asymptotic growth.
What you can do after
Evaluate algorithm performance by identifying work that scales with input size and separating time from extra space.
How it connects
Connects Big-O notation, linear search, space complexity, the RAM model, and dynamic arrays.

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 Data Structures. The section links point to the parts of the book it draws on.