How can I find the time complexity of an algorithm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding the time complexity of an algorithm is fundamental to understanding its efficiency and performance. The time complexity gives us an upper bound on the time an algorithm takes to process an input as a function of the size of that input. Here’s a detailed guide on how to determine the time complexity of an algorithm, complete with technical explanations and examples.
1. Analyze the Algorithm
Before calculating time complexity, it's essential to carefully analyze the algorithm. This involves understanding the operations performed and how they vary with input size.
Example
Consider a simple loop iterating through an array:
2. Count the Basic Operations
The next step is to identify basic operations and see how their number scales with input size, . In the example above, assume that comparing elements and incrementing the loop counter are basic operations.
Steps to Count Basic Operations
- Initialization: This occurs once.
- Loop Execution: The loop runs for times.
- Therefore, basic operations inside the loop approximate to .
- Conditional Checks: Each iteration has a constant time conditional check.
- due to the loop running times.
Key Points on Operations
| Operation | Count | Time Complexity |
| Initialization | 1 | |
| Loop Iteration | ||
| Conditional Check | ||
| Total | - |
3. Classify the Time Complexity
The primary task is to combine these operations to get the total time complexity. In the example above, the dominant term is the loop, giving us .
Common Time Complexities
- Constant Time: - Time taken does not vary with input size.
- Logarithmic Time: - Typically found in algorithms that divide problems in half, such as binary search.
- Linear Time: - Time grows linearly with input size, like iterating over a list.
- Linearithmic Time: - Common in efficient sorting algorithms like mergesort.
- Quadratic Time: - Nested loops over the data set, such as bubble sort.
4. Best, Worst, and Average Case Analysis
When assessing algorithms, consider different scenarios:
- Best Case: Minimum operations performed, e.g., sorted data in a sorting algorithm.
- Worst Case: Maximum operations, essential for ensuring the algorithm's upper limit.
- Average Case: Expected performance for a random distribution of inputs.
Example for Best, Worst, and Average Cases
Take binary search:
- Best Case: if the target is at the mid-point initially.
- Worst Case: when zeroing in over multiple iterations.
- Average Case: assuming random distribution.
5. Simplifying Time Complexity
It's crucial to express time complexity in its simplest form. Focus on the highest order of because it dominates the growth rate as increases.
Example of Simplification
Suppose you determine an algorithm has . For large :
- Ignore constants and lower-order terms.
- Simplified as .
6. Use of Recurrence Relations
For recursive algorithms, use recurrence relations to assess time complexity. This is particularly relevant in divide-and-conquer algorithms such as mergesort.
Example with Recurrence Relations
Consider the mergesort algorithm with a time complexity of . By applying the Master Theorem, we determine:
- .
Conclusion
Analyzing the time complexity of an algorithm involves understanding basic operations, evaluating best/worst case scenarios, and expressing the result concisely. This analysis is intrinsic to optimizing algorithm performance and ensuring efficient computational resources usage.
By grasping these principles, programmers can make informed choices about which algorithm to use based on the expected input size and other performance criteria. Developing proficiency in this area is an invaluable skill for both academic pursuits in computer science and practical software development.
Summary Table: Core Concepts
| Concept | Description |
| Basic Operations | Core actions an algorithm executes based on input size. |
| Common Complexities | , , , , |
| Best/Worst/Average Case | Scenario-specific performance evaluations. |
| Recurrence Relations | Used for recursive algorithm complexity analysis. |
| Simplification Principles | Ignoring constants and lower-order terms. |
Understanding these principles equips developers and students alike with the tools needed to evaluate and refine algorithms in pursuit of optimization.
Related reading
- How can I generate all permutations of an array in Perl?
- How can I generate sorted uniformly distributed random numbers efficiently in C?
- How can I get the most frequent 100 numbers out of 4,000,000,000 numbers?
- How can I implement a queue using two stacks?
- How can I get the size of a MySQL database?
- How can I get the sizes of the tables of a MySQL database?
- How can I improve this algorithm for solving a modified Postage Stamp puzzle?
- How can I interleave or create unique permutations of two strings without recursion

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.