Olog N O1 - Why not?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding the intricacies of algorithmic complexity can often be challenging, especially when it comes to differentiating between supposedly similar time complexities like and . On the surface, one might argue that in practical situations, and perform similarly. In this article, we'll explore why they are fundamentally different and why they cannot be considered equivalent.
Big O Notation Primer
Before delving into the comparison, it's important to understand what and signify in Big O notation. Big O notation is used to classify algorithms based on how their running time or space requirements grow as the input size grows.
- : Known as constant time complexity, it implies that the running time of an algorithm is constant and does not change with the size of the input data. An example of complexity is accessing an element in an array by index.
- : This complexity represents a logarithmic growth rate. It means that the algorithm's time complexity increases logarithmically as the input size increases. A classic example is the binary search algorithm.
Why
Theoretical Basis
- Growth Rates:
- For , the runtime is invariant to input size (e.g., a simple operation or a direct retrieval).
- For , the runtime grows logarithmically with input. This suggests that even though the growth is slow, it is not constant.
- Logarithmic Behavior:
- Logarithmic growth occurs in problems that can be divided in half repeatedly, such as in binary search trees or when evaluating Exponentiation by Squaring.
- Bounded Comparisons:
- Mathematically, as approaches infinity, the difference between and a constant value becomes evident since increases without bound, albeit slowly.
Practical Scenarios
Consider a binary search on a sorted array of `N` elements, which is . Contrast this with directly accessing an element in an array, which is . For small input sizes in practice, might appear similar to , but as the dataset grows, the discrepancy between their efficiencies becomes more pronounced.
Computational Costs
Another aspect to consider is computational costs:
- : Involves basic operations with no dependency on input size.
- : Involves operations that iterate a stepwise or halving process relative to input size, demanding more CPU cycles for larger inputs.
Real-World Implications
In real-world systems, algorithms often seem instantaneous due to the small growth factor of logarithms. However, scenarios like network latency, disk reads, or real-time systems necessitate true constant time guarantees. Consider operations in a real-time application, where predictable performance is essential, and may be requisite to meet guaranteed response times.
Summary of Differences
| Complexity Class | Description | Example Scenarios | Growth Nature |
| Constant time. The runtime does not depend on the size of the input. | Array index access, hash table look-ups (in the best-case scenario) | Flat (no growth) | |
| Logarithmic time. The runtime grows logarithmically as the input size increases. | Binary search, balanced tree operations, Exponential functions by division | Slowly increasing |
Conclusion
While on a superficial level, and might seem similar in terms of time efficiency for smaller input sizes, they are fundamentally different when considered in-depth. provides a time complexity that remains unchanged regardless of input size, making it truly constant. Conversely, indicates a slowly increasing computational requirement that scales with input size, however marginal it may be. Ensuring accurate understanding and application of these complexities is crucial for optimal algorithm performance and system design.
Related reading
- On-line iterator algorithms for estimating statistical median, mode, skewness, kurtosis?
- ON algorithm slower than ON logN algorithm
- On algorithm to find the median of n² implicit numbers
- On log log n time complexity
- On duplicate key ignore?
- On log n vs On -- practical differences in time complexity
- On of solution to solve boggle
- On On On?

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.