Logarithmic Time Complexity

Logarithmic time complexity with example

Logarithmic time complexity (O(log n)) refers to algorithms that reduce the problem size significantly with each step, typically by half. These algorithms are very efficient, especially for large inputs, as they don't need to process every single element.

When an algorithm has a time complexity of (O(log n)), it means that as the input size ( n ) increases, the number of operations grows logarithmically. In simple terms, doubling the size of the input only increases the number of operations by one step.

The most common example of a logarithmic time complexity algorithm is binary search.

Binary Search Example:

Suppose you have a sorted list of numbers:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Let's say you want to find the position of the number 9 in this list.

How Binary Search Works:

  1. Step 1: Check the middle element.
    • Middle element: 11 (position 5)
    • Since 9 < 11, ignore the right half and focus on the left half.
  2. Step 2: Repeat the process on the left half.
    • New list: [1, 3, 5, 7, 9]
    • Middle element: 5 (position 2)
    • Since 9 > 5, ignore the left half and focus on the right half.
  3. Step 3: Repeat on the remaining part.
    • New list: [7, 9]
    • Middle element: 7 (position 3)
    • Since 9 > 7, move to the next half.
  4. Step 4: Only one element remains.
    • Element 9 found at position 4.

Why is Binary Search O(log n)?

  • Every time, binary search cuts the list size in half.
  • For a list of size ( n ), you can cut it in half approximately log₂(n) times until you're left with one element.
  • For example:
    • A list of size 16: binary search will take at most 4 steps (log₂(16) = 4).
    • A list of size 1,000,000: binary search will take at most 20 steps (log₂(1,000,000) ≈ 20).

Real-Life Analogy

Imagine a book with 1,000 pages. You’re looking for a specific word that you know is in alphabetical order. Instead of flipping through every page one by one:

  1. You open the book roughly in the middle.
  2. Check if the word you’re searching for is before or after the middle page.
  3. Open the middle of the remaining section.
  4. Repeat this process until you find the word.

By using this approach, you quickly narrow down your search space without having to look at every single page, making your search much faster!