0% found this document useful (0 votes)
10 views3 pages

Guide - How To Measure Time Complexity

This guide provides a comprehensive overview of time complexity measurement, detailing various patterns in loops, recursion, divide and conquer, dynamic programming, graph algorithms, sorting algorithms, logarithmic complexities, and special cases. Each section includes code examples or recurrence relations along with their corresponding time complexities. It serves as a quick reference for analyzing the efficiency of algorithms.

Uploaded by

Nandan Patkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Guide - How To Measure Time Complexity

This guide provides a comprehensive overview of time complexity measurement, detailing various patterns in loops, recursion, divide and conquer, dynamic programming, graph algorithms, sorting algorithms, logarithmic complexities, and special cases. Each section includes code examples or recurrence relations along with their corresponding time complexities. It serves as a quick reference for analyzing the efficiency of algorithms.

Uploaded by

Nandan Patkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

GUIDE : HOW TO MEASURE TIME COMPLEXITY

🚀
This guide should help you quickly analyze time complexity by
recognizing common patterns in code!

1. Loop-Based Patterns
Pattern Code Example Time
Complexity
Single Loop for (int i = 0; i < n; i++) O(n)
Nested Loops for (int i = 0; i < n; i++) O(n²)
for (int j = 0; j < n; j++)
Triple Nested Loops for (int i = 0; i < n; i++) O(n³)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
*Loop with Increment (i = for (int i = 1; i < n; i *= 2) O(log n)
2, i += constant)
Loop with Decrement (i /= for (int i = n; i > 0; i /= 2) O(log n)
2)

2. Recursive Patterns
Pattern Recurrence Relation Time Complexity
Linear Recursion T(n) = T(n-1) + O(1) O(n)
Binary Recursion T(n) = 2T(n/2) + O(1) O(n)
Divide & Conquer (Merge T(n) = 2T(n/2) + O(n) O(n log n)
Sort, Quick Sort Worst
Case)
Exponential Recursion T(n) = T(n-1) + T(n-2) O(2ⁿ)
(Fibonacci, Brute Force
DFS)
3. Divide and Conquer Patterns
Algorithm Recurrence Complexity
Binary Search T(n) = T(n/2) + O(1) O(log n)
Merge Sort T(n) = 2T(n/2) + O(n) O(n log n)
Quick Sort (Best & Avg) T(n) = T(n/2) + O(n) O(n log n)
Quick Sort (Worst Case - T(n) = T(n-1) + O(n) O(n²)
sorted array)

4. Dynamic Programming Patterns


Pattern Example Time Complexity
Memoization (Top-Down Fibonacci DP O(n)
Recursion with Cache)
Bottom-Up Iterative DP Knapsack, LIS O(n²) or O(n³)
Matrix Chain Multiplication T(n) = O(n³) O(n³)

5. Graph Algorithms
Algorithm Complexity
BFS / DFS (Adjacency List) O(V + E)
Dijkstra (Min Heap) O((V + E) log V)
Bellman-Ford O(VE)
Floyd Warshall (All-Pairs O(V³)
Shortest Path)
Prim’s / Kruskal’s MST O(E log V)
6. Sorting Algorithms
Algorithm Best Worst Case
Case
Bubble Sort / O(n) O(n²)
Insertion Sort
Merge Sort O(n log O(n log n)
n)
Quick Sort O(n log O(n²)
n)
Heap Sort O(n log O(n log n)
n)

7. Logarithmic and Amortized Complexities


Pattern Example Complexity
Binary Search Search in O(log n)
sorted array
Heap Operations Priority Queue, O(log n)
(Insert/Delete) Dijkstra
Balanced BST (Insertion, AVL, Red-Black O(log n)
Deletion, Search) Tree
Union-Find (Path Compression DSU operations O(α(n)) (inverse
& Rank) Ackermann)

8. Special Cases
Case Exam
ple
Iterating All Subsets O(2ⁿ)
Iterating All O(n!)
Permutations
Brute Force Checking O(n²)
All Pairs

You might also like