How to know when Big O is Logarithmic?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In understanding algorithm efficiency and performance, Big O notation is a critical concept. It provides a way to classify algorithms according to their runtime or space complexity in the worst-case scenario. One commonly encountered complexity is logarithmic, denoted as . This article explores how to identify when an algorithm exhibits logarithmic time complexity.
Characteristics of Logarithmic Complexity
Before determining if an algorithm's time complexity is logarithmic, it's essential to understand the fundamental properties and characteristics of complexity:
- Divide and Conquer: When an algorithm reduces the problem size by a factor (usually half) on each iteration or recursive call.
- Balanced Trees: Operations on balanced tree structures (like binary search trees or AVL trees) often involve logarithmic time complexity due to their hierarchical nature.
- Binary Decisions: Algorithms making binary decisions in their logic or processing generally result in a logarithmic number of steps.
Mathematical Explanation
An algorithm has a complexity of if as the input size increases, the number of operations grows logarithmically. This generally involves two main classes:
• : Common in binary operations, such as binary search. • : This base is rare in computer science but can occur in scenarios where a problem divides by ten.
Common Scenarios
- Binary Search: Perhaps the most classic example of complexity. This algorithm divides a sorted collection in half to find a target value. Each division operation removes half of the remaining elements from consideration.
- Balanced Tree Operations: In structures like AVL trees or Red-Black trees, search, insertion, and deletion operations have due to the height of the tree being balanced as logarithmic.
- Heap Operations: The operations to insert an element or remove the minimum element in a binary heap have logarithmic time complexity.
- Exponentiation by Squaring: An efficient method for computing large powers of a number, reducing the number of multiplication operations from linear to logarithmic.
Identifying Logarithmic Complexity
To identify if an algorithm's complexity is logarithmic, look for: • Problem size reducing by a fixed fraction at each step. • Subproblems being independent. • The structure of the problem suggesting hierarchy or binary-like division.
Example: Binary Search
Consider the following pseudocode for binary search:
Related reading
- How to logically interpret any variation of binary search
- How to loop through all the files in a directory in c .net?
- How to make an efficient solver for Puzzle Number 9
- How to map hashfunction output to bloomfilter indices?
- How to lazy load images in ListView in Android
- How to let the UI refresh during a long running UI operation
- How to master in-place array modification algorithms?
- How to match a tree against a large set of patterns?

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 courseTrack 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.