Big-O complexity of a piece of code
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 Big-O complexity of a piece of code is crucial for analyzing its efficiency, especially as the size of the input grows. This article will delve into the concept, providing technical explanations and examples to elucidate the topic.
Introduction to Big-O Notation
Big-O notation is a mathematical representation that describes the upper bound of an algorithm's running time or space requirements in terms of the input size. It characterizes algorithms by their growth rate, facilitating a high-level understanding of performance efficiency. The primary focus in Big-O analysis is on the input size affecting the performance as it approaches infinity.
Big-O Complexity Classes
Several standard complexity classes are typically used to describe algorithms:
- Constant Time - :
- The running time is unaffected by the size of the input.
- Example: Accessing an element in an array by index.
- Logarithmic Time - :
- The running time increases logarithmically with the input size.
- Example: Binary search on a sorted array.
- Linear Time - :
- The running time grows linearly with the input size.
- Example: Iterating through an array of `n` elements.
- Linear Logarithmic Time - :
- Common in sorting algorithms like merge sort or quicksort.
- Example: Sorting elements using an efficient sorting algorithm.
- Quadratic Time - :
- Running time is proportional to the square of the input size.
- Example: A simple two-dimensional nested loop.
- Cubic Time - :
- Running time scales cubically with input size.
- Example: Triple nested loops over `n`.
- Exponential Time - :
- Grows exponentially as input increases.
- Example: Solving the traveling salesman problem via brute force.
- Factorial Time - :
- Infeasible for large `n` due to rapid growth.
- Example: Permutations of a set.
Analyzing a Piece of Code
Let's analyze the Big-O complexity of a simple code snippet:
- Setting `sum = 0` is a constant time operation, .
- The `for` loop executes `n` times (where `n` is the length of `arr`), having a time complexity of .
- The function's overall complexity is . However, Big-O notation focuses on the term with the highest growth rate, ignoring lower-order terms and coefficients. Thus, the complexity simplifies to .
Related reading
- Big-O for Eight Year Olds?
- Big-O for various Fibonacci Implementations
- Big-O of log versus square root
- Big-O summary for Java Collections Framework implementations?
- Big-oh vs big-theta
- Big O - Ologn code example
- Big O complexity of the basic arithmetic operations
- Big O for worst-case running time and Ω is for the best-case, but why is Ω used in worst case sometimes?

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.