algorithm
logarithmic
complexity
big-o
code-example

Big O - Ologn code example

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.