Why a programmer would prefer ON3 instead of ON2
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
A programmer's choice of algorithm complexity isn't always about striving for the lowest possible time complexity. There are instances where a programmer might intentionally choose an algorithm over an algorithm. This article examines the reasoning and scenarios where such a choice is justified, supported by technical explanations and examples.
Understanding Time Complexity
Time complexity is a function that describes how the execution time of an algorithm grows with the input size, . An algorithm with a lower time complexity is generally preferred, as it performs more efficiently for large inputs. However, other factors can make a more computationally complex algorithm a better choice.
Situations Favoring over
- Algorithmic Simplicity and Maintainability
An algorithm might be more straightforward to understand and implement than a more sophisticated algorithm. If readability and maintainability are priorities—such as in a teaching environment or a rapid prototyping phase—choosing the more understandable approach could be beneficial. - Exact Solutions vs. Heuristics
Sometimes, algorithms are heuristic or approximation methods, while versions guarantee an exact solution. In scenarios where precision is critical, the exact solution provided by the algorithm might outweigh the performance drawbacks. - Memory Constraints
An algorithm might have unacceptable memory demands that scale poorly with input size. If the algorithm has a more favorable space complexity, it could be preferred despite increased time complexity. - Specific Problem Domains
Problems rooted in computational theory, such as matrix multiplication, often use algorithms with time complexities like due to the nature of operations involved. Consider the naive approach to matrix multiplication, which is , providing a simple solution even if it's not the most optimal. - Problem Size and Context
For small inputs, the difference in performance between an and algorithm might be negligible. If the problem domain involves consistently small datasets, the simplicity or other benefits of the approach might be more valued.
Case Study: Matrix Multiplication
Matrix multiplication is a classic example where an approach is often adequate. The naive algorithm entails three nested loops to compute the result, which is easily understood and implemented.
- Complexity: Does the increased complexity of a more efficient algorithm introduce risks or delays in development?
- Performance Needs: Are the datasets large enough that the difference in time complexity significantly impacts performance?
- Environmental Constraints: Do system limitations (e.g., memory, processor capabilities) make one approach more viable than another?
Related reading
- Why are back edges required in the Ford-Fulkerson algorithm?
- Why are Fibonacci numbers significant in computer science?
- Why are hash table expansions usually done by doubling the size?
- Why are heaps in c implemented as algorithms instead of containers?
- Why Aeron multicast is much slower than raw Tcp implementation
- Why are Docker container images so large?
- Why Arrays.sort is quicksort algorithm, why not another sort algorithm?
- Why best case for insertion sort is On not On2?

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.