AlgoViz

Logarithms

easy 14 min

A logarithm answers "what power do I raise the base to?" In code, log2 n is how many times you can halve n before reaching 1, which is why binary search, balanced trees and divide and conquer all run in O(log n) and barely slow down as data grows.

Where it shows up

The same pattern appears across everyday software.

  • Database indexes

    B-tree and B+ tree indexes branch hundreds of ways per node, so finding one row among a billion takes only three or four page reads. That is a logarithm with a very large base.

  • Git bisect

    To find the commit that introduced a bug among 1,000 commits, git bisect tests the middle commit and halves the range each time, needing about 10 test runs instead of up to 1,000.

  • Standard library search

    Python's bisect module, C++'s std::lower_bound and std::binary_search, and Java's Arrays.binarySearch all run binary search in O(log n) on sorted data.

  • Priority queues and schedulers

    Operating system schedulers, Dijkstra's shortest path and event simulators use heaps, where each insert or extract walks one path of a tree of height about log2 n.

  • Fast exponentiation in cryptography

    RSA and Diffie-Hellman raise numbers to exponents hundreds of digits long. Square-and-multiply needs only about log2 of the exponent multiplications, which makes them practical.

  • Sound and signals

    Decibels and musical octaves are logarithmic scales: every octave doubles the frequency, just as every level of a binary tree doubles the node count.