OLogN Logarithmic Time

1 min read
Foundational1 min read
Rapid overview

OLogN Logarithmic Time

TL;DR

O(log n) logarithmic time means each step discards a constant fraction of the remaining work, so cost barely grows even as input explodes — a million elements take only ~20 steps. It's the complexity of binary search and balanced-tree or heap operations, and the reason sorted data is so valuable: it unlocks logarithmic lookups instead of linear scans.

How it works

const binarySearch = (items: number[], target: number) => {
  let low = 0;
  let high = items.length - 1;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    const value = items[mid];
    if (value === target) return mid;
    if (value < target) low = mid + 1;
    else high = mid - 1;
  }
  return -1;
};

Notes

  • Binary search works only on sorted data.
  • Heap operations are O(log n).

See also