liveBook · Manning

Linear time complexity

As explained in Learn C++ by Example

Linear complexity is an algorithmic growth rate proportional to the number of input elements, commonly expressed as O(n).

Linear time complexity describes work that grows in proportion to the number of input elements, commonly written as O(n). Searching a vector may require checking every element, including the final one or none at all. section 7.2.1

The comparison with map search highlights the practical cost of growth:

  • A vector stores elements contiguously, so an unsuccessful search or a late match can inspect all n elements. section 7.2.1
  • A map organizes key-value pairs in an ordered binary search tree, allowing a search to discard part of the tree at each step. section 7.2.1

Searching a vector

With n elements, checking items from the beginning until finding a match may require n comparisons. If no match exists, the search reaches the end and still performs work proportional to n. section 7.2.1

Figure 7.3 Constant time complexity: O(n) grows much faster than logarithmic complexity, O(log(n)), as the number of elements is increased.
CH07_F03_Buontempo

Visualizes how O(n) grows faster than O(log(n)) as the number of elements increases. section 7.2.1

How Learn C++ by Example covers it

How deep it goes
Defines O(n) through a worst-case vector search and contrasts it with tree-based searching without deriving formal bounds.
Examples
Uses searching a vector containing n elements and compares it with searching ordered map nodes.
What it assumes
Assumes familiarity with vectors, arrays, and maps while explaining their storage and search behavior.
What you can do after
Evaluate how input growth affects search work and compare sequential searching with tree-based lookup.
How it connects
Connects linear complexity with vectors, arrays, maps, binary search trees, and logarithmic complexity.

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 Learn C++ by Example. The section links point to the parts of the book it draws on.