algorithm
logarithmic
complexity
big-o
code-example

Big O - Ologn code example

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding Big O Notation: O(log n) with a Code Example

When analyzing algorithms, particularly those that deal with large data sets, understanding their efficiency is crucial. Big O notation serves as a fundamental tool to describe how the run time of an algorithm grows with the size of the input. One common complexity class is `O(log n)`, which represents algorithms that take logarithmic time relative to their input size. In this article, we delve into `O(log n)` complexity, explore its significance, and present a code example to illustrate its application.

Understanding Logarithmic Complexity

The `O(log n)` complexity often arises in algorithms that systematically reduce the problem size with each step. The term `log n` typically implies the base-2 logarithm, but in Big O notation, base doesn't matter since logarithms of different bases are only a constant factor apart.

What Does `O(log n)` Mean?

  • Logarithm Basics: The logarithm of a number is the exponent to which the base must be raised to produce that number. In this context, `log n` asks: "To what power must 2 be raised to obtain n?"
  • Halving Principle: If an algorithm is `O(log n)`, it usually implies that the algorithm halves the input size at each step, thereby needing only a logarithmic number of steps relative to the input size.

Common `O(log n)` Algorithms

  • Binary Search: Perhaps the most well-known `O(log n)` algorithm, binary search operates on a sorted array by dividing the search space in half with each step until the desired element is found or determined to be absent.
  • Balanced Trees: Data structures like AVL trees and Red-Black trees maintain their balance properties, ensuring their operations (insert, delete, find) are `O(log n)` since they effectively halve the problem space at each node.
  • Fast Exponentiation: Also known as exponentiation by squaring, fast exponentiation reduces the number of multiplicative operations required to compute power, making it logarithmic in terms of time complexity.

Binary Search: A Classic Example

We’ll use binary search as an example to demonstrate `O(log n)`:

  • If it equals the target, the search is complete.
  • If the target is greater, search in the right half.
  • Otherwise, search in the left half.

Course illustration
Course illustration

All Rights Reserved.