liveBook · Manning

Logarithmic time complexity

As explained in Learn C++ by Example

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

Logarithmic time complexity describes work that grows with the logarithm of the number of elements, commonly written O(log(n)). A balanced binary search tree reaches a target by choosing only one branch at each comparison, so doubling the elements requires only about one additional set of comparisons. section 7.2.1

Searching a map

To find key 2 in a balanced tree containing 1, 3, and 5, begin at 3, move left to 1, and stop when the search path ends. An std::map commonly uses this structure, giving search, insertion, and removal logarithmic complexity. By contrast, searching a vector is O(n): in the worst case, every element must be checked. section 7.2.1 section 7.4

Figure 7.2 Three elements in a balanced binary search tree: nodes with smaller keys go on the left and larger keys on the right.
CH07_F02_Buontempo

See how smaller keys occupy the left branch and larger keys the right branch in a balanced binary search tree. section 7.2.1

How Learn C++ by Example covers it

How deep it goes
Explains branch-by-branch search, doubling behavior, and tree balancing, but does not derive logarithms mathematically.
Examples
Uses a three-node search path and contrasts map lookup with worst-case vector scanning.
What you can do after
Evaluate data-structure choices by comparing logarithmic map operations with linear vector searches.
How it connects
Connects logarithmic complexity to std::map, balanced binary search trees, red-black trees, vectors, and linear complexity.

Also explained in

Compare how 4 books explain logarithmic 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.