How is On log n different then Olog n?
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 Complexity Classes: vs.
Time complexity is a critical concept in computer science, used to estimate the amount of time an algorithm takes to run relative to the size of its input. Two common complexity classes are and . Although they may seem similar, these represent fundamentally different growth rates, impacting algorithm performance and scalability. Below, we explore their differences, applications, and implications.
Technical Explanation
- : Logarithmic Complexity • Growth Rate: An algorithm with complexity increases logarithmically with the input size. This means for every doubling of the input size, the operation count increases by a constant number. • Common Use Cases: Primarily seen in algorithms where the problem size reduces dramatically with each step. Examples include binary search and operations on balanced trees like AVL trees. • Mathematical Explanation: The function grows much slower than linear functions. For example, the base-2 logarithm can be visualized as the number of times you can halve until you reach 1.
- : Linearithmic Complexity • Growth Rate: This complexity arises in algorithms that involve a combination of linear and logarithmic operations. The execution time grows in proportion to the size of the input multiplied by the logarithm of the input size. • Common Use Cases: Frequently found in efficient sorting algorithms like Merge Sort, Quick Sort (average case), and Heap Sort. Also appears in some divide and conquer strategies. • Mathematical Explanation: It can be viewed as subproblems, each taking approximately time to solve. Thus, grows faster than , yet slower than quadratic complexities like .
Key Differences and Implications
| Aspect | ||
| Growth Rate | Logarithmic, very slow increase | Linearithmic, moderate increase |
| Algorithm Type | Search/ reduction (e.g., Binary Search) | Sorting, divide and conquer (e.g., Merge Sort) |
| Operational Cost | Reduces problem size rapidly | Solves subproblems with logarithmic reduction |
| Scalability | Highly scalable, handles large inputs | Good scalability, but higher overhead compared to |
| Example Expression |
Detailed Breakdown of Use Cases
• Binary Search (O(log n)): Utilizes a divide-and-conquer approach by halving the problem space during each step. Ideal for searching in large sorted datasets efficiently.
• Merge Sort (O(n \log n)): Divides the array into halves, recursively sorts each half, and then merges the sorted halves. The division gives the logarithmic part while merging requires linear comparison.
Additional Considerations
• Constants and Base of Logarithm: In complexity analysis, the base of the logarithm in is irrelevant for large . Both expressions, and , are equivalent. Similarly, is independent of base, emphasizing its generality over scales.
• Performance and Practicality: While theoretical complexity provides big-picture insights, real-world performance is also impacted by other factors like constant-time operations, data structure overhead, and memory utilization.
• Algorithm Selection: Recognizing when to use each complexity type is crucial for problem solving. For instance, choosing a binary search over a linear search provides significant time savings in sorted datasets, while sorting algorithms ensure consistent efficiency across varied inputs.
Conclusion
Understanding the distinctions between and is pivotal for designing optimal algorithms. While both are efficient for different use cases, recognizing their unique characteristics and growth patterns enables better decision-making, ultimately contributing to more efficient software solutions. Whether you're searching, sorting, or designing complex systems, a solid grasp of these complexity classes enhances both theoretical understanding and practical implementation.
Related reading
- How is pagerank calculated in a distributed way?
- How is quick sort better at cache locality than mergesort?
- How is str.joiniterable method implemented in Python/ Linear time string concatenation
- How is strong consistency possible given two generals problem
- How is performance affected by an unused using directive?
- How is tf.data.Dataset use optimised by tf.function in Tensorflow 2.0?
- How is the complexity of bucket sort is Onk if we implement buckets using linked lists?
- How is the complexity of PCA Ominp3,n3?

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.