dotnet new console -n csharp
- Big O Cheat Sheet{:target="_blank"}
- Halting Problem{:target="_blank"}
- Big O Notation{:target="_blank"}
- Asymptotic Analysis{:target="_blank"}
- Polynomial{:target="_blank"}
- Stack Overflow: Big O Calculation{:target="_blank"}
Time complexity is a measure of the computational complexity that describes the amount of time an algorithm takes to run as a function of the size of its input. Here’s a step-by-step guide to determine the time complexity of an algorithm:
- Carefully analyze the algorithm or code.
- Identify the input size
n
. - Determine what the algorithm does with the input (loops, function calls, etc.).
- Count the most frequently executed operation, such as arithmetic operations, comparisons, data assignments, or function calls.
- Focus on the part of the code that has the largest impact on the running time.
- Simple Loops: A loop running
n
times contributes ( O(n) ). - Nested Loops: Multiply the iterations. For example:
- A loop inside another loop, each running
n
times, results in ( O(n^2) ).
- A loop inside another loop, each running
- Logarithmic Loops: A loop that halves the input size in each iteration contributes ( O(\log n) ).
- Use recurrence relations to model recursive calls.
- Solve the recurrence using methods like the Master Theorem or Substitution Method.
- Example: A recursive algorithm splitting the problem into two halves (like merge sort) often has a complexity of ( O(n \log n) ).
- If the algorithm has multiple independent steps, sum their complexities.
- Example: ( O(n) + O(n^2) ) simplifies to ( O(n^2) ) (higher-order term dominates).
- For conditional branches (
if
,else
), analyze the worst-case complexity.
- Time complexity focuses on the growth rate, so constants and non-dominant terms can be omitted.
- Example: ( O(2n + 10) ) simplifies to ( O(n) ).
- Big-O notation describes the upper bound of an algorithm’s growth rate.
- Examples:
- ( O(1) ): Constant time.
- ( O(\log n) ): Logarithmic time.
- ( O(n) ): Linear time.
- ( O(n^2) ): Quadratic time.
- Test the algorithm with various input sizes.
- Ensure the theoretical complexity matches the observed behavior.
Notation | Example Algorithms | Growth Rate |
---|---|---|
( O(1) ) | Accessing an array element | Constant |
( O(\log n) ) | Binary search | Logarithmic |
( O(n) ) | Linear search | Linear |
( O(n \log n) ) | Merge sort, Quick sort (avg) | Log-linear |
( O(n^2) ) | Bubble sort, Selection sort | Quadratic |
( O(2^n) ) | Recursive Fibonacci | Exponential |
By following these steps, you can systematically determine the time complexity of any algorithm.