Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Time and Space Complexity Tutorial

Uploaded by

focusedswirles1
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)
5 views2 pages

Time and Space Complexity Tutorial

Uploaded by

focusedswirles1
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/ 2

Time and Space Complexity - Big O Notation Guide

What is Time and Space Complexity?


- Time Complexity: How the runtime of an algorithm grows with input size n.
- Space Complexity: How the memory usage of an algorithm grows with input size n.

Big O Notation Basics


- O(1): Constant Time -> Accessing array by index
- O(log n): Logarithmic Time -> Binary Search
- O(n): Linear Time -> Loop through array
- O(n log n): Linearithmic -> Merge Sort, Quick Sort (avg)
- O(n**2): Quadratic -> Nested loops (Bubble Sort)
- O(2**n): Exponential -> Recursive Fibonacci
- O(n!): Factorial -> Brute-force permutations

Time Complexity Examples


- O(1): Access array element
- O(n): Sum array elements
- O(n**2): Print all pairs in array (nested loop)
- O(log n): Binary Search

Space Complexity Examples


- O(1): Finding max
- O(n): Copying array

Loop Complexity
- for i in range(n): O(n)
- for i in range(n): for j in range(n): O(n**2)
- for i in range(1, n, i*=2): O(log n)

Drop Constants and Lower Terms


- O(2n) -> O(n), O(n + log n) -> O(n), O(n**2 + n) -> O(n**2)
Case Analysis
- Best Case: Fastest (e.g. Binary Search O(1))
- Worst Case: Slowest (e.g. Binary Search O(log n))
- Average Case: Expected runtime

Recursion
Example: Fibonacci -> Time: O(2**n), Space: O(n)

Algorithm Cheat Sheet


- Linear Search: O(n)
- Binary Search: O(log n)
- Bubble Sort: O(n**2)
- Merge Sort: O(n log n)
- Quick Sort (avg): O(n log n)
- Insertion Sort: O(n**2)
- Hashing Lookup: O(1) avg, O(n) worst

Tips
- Dry run your code.
- Count operations in loops.
- Practice on GFG, LeetCode.
- Learn DS internal complexities.

You might also like