You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A comprehensive guide to understanding the time and space complexities of common algorithms and data structures. This repository provides a concise summary of the key concepts in algorithm analysis, presented in an easy-to-read cheat sheet format.
Notifications You must be signed in to change notification settings
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go to fileWelcome to the "Big-O Complexity Cheat Sheet" repository! This cheat sheet is designed to provide a quick reference guide for understanding the time and space complexity of various algorithms and data structures. As a developer, you will often encounter problems that require efficient solutions, and having a solid understanding of Big O notation is essential for writing performant code. In this repository, you will find a comprehensive list of common algorithms and data structures, along with their time and space complexities. This will serve as a handy resource for developers, computer science students, and anyone interested in learning more about the fundamental concepts of computer science. Whether you are preparing for a technical interview or simply want to improve your knowledge of algorithmic complexities, this cheat sheet is the perfect starting point for your journey.
A very useful complexity chart by:
Complexity | Description | Example |
---|---|---|
Constant Time | O(1) - Constant, regardless of input size | Accessing an array element by index |
Logarithmic Time | O(log n) - Increases logarithmically with input size | Binary search |
Linear Time | O(n) - Increases linearly with input size | Iterating through an array (no loops) |
Linearithmic Time | O(n log n) - Linearly with input size * logarithmic factor | Merge sort |
Quadratic Time | O(n²) - Increases quadratically with input size | Nested loops (2 loops) |
Cubic Time | O(n³) - Increases cubically with input size | Triple nested loops (3 loops) |
Exponential Time | O(2^n) - Increases exponentially with input size | Naive recursive Fibonacci |
Factorial Time | O(n!) - Increases factorially with input size | Generating all possible permutations |
def get_first(my_list): return my_list[0]
# Binary search
# Iterating through an array def print_all_elements(my_list): for element in my_list: print(element)
# Merge sort
# 2 nested for loops def print_all_possible_ordered_pairs(my_list): for first_item in my_list: # O(n) for second_item in my_list: # O(n) print(first_item, second_item)
# 3 nested for loops -- Also use as last resort for 3D arrays def naive_matrix_mult(A, B): rows_A, cols_A = len(A), len(A[0]) rows_B, cols_B = len(B), len(B[0]) if cols_A != rows_B: raise ValueError("Matrices cannot be multiplied.") C = [[0 for _ in range(cols_B)] for _ in range(rows_A)] for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] += A[i][k] * B[k][j]
# Iterating through all subsets of a set def print_all_subsets(my_set): all_subsets = [[]] for element in my_set: for subset in all_subsets: all_subsets = all_subsets + [list(subset) + [element]] return all_subsets # or def naive_fibonacci(n): if n 1: return n return naive_fibonacci(n - 1) + naive_fibonacci(n - 2)
# Iterating through all permutations of a set def generate_permutations(arr, start=0): if start == len(arr) - 1: print(arr) for i in range(start, len(arr)): arr[start], arr[i] = arr[i], arr[start] # Recurse generate_permutations(arr, start + 1) arr[start], arr[i] = arr[i], arr[start]
def print_all_elements(my_list): for element in my_list: print(element)
# O(n) space - Storing all elements in a hash table def reverse_list(arr): reversed_arr = [] for i in range(len(arr) - 1, -1, -1): reversed_arr.append(arr[i]) return reversed_arr
# O(n^2) space - Storing all elements in a 2D array def create_identity_matrix(n): identity = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): identity[i][i] = 1 return identity
# Exponential Space - O(2^n) def power_set(arr): result = [[]] for item in arr: result += [subset + [item] for subset in result] return result
A comprehensive guide to understanding the time and space complexities of common algorithms and data structures. This repository provides a concise summary of the key concepts in algorithm analysis, presented in an easy-to-read cheat sheet format.