0% found this document useful (0 votes)
39 views

Cheat Sheet

Uploaded by

Ai Cha
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)
39 views

Cheat Sheet

Uploaded by

Ai Cha
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

Key

Good Fair Poor

Data Structures
Space
Data Structure Time Complexity Complexity

Average Worst Worst

Indexing Search Insertion Deletion Indexing Search Insertion Deletion

Basic Array (Array) O(1) O(n) ­ ­ O(1) O(n) ­ ­ O(n)

Dynamic Array (List<T>


O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
and ArrayList)

Singly­Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)

Doubly­Linked List
O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
(LinkedList<T>)

Skip List O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))

Hash Table (HashSet<T>


Dictionary<TKey, TValue> ­ O(1) O(1) O(1) ­ O(n) O(n) O(n) O(n)
and Hashtable)

Binary Search Tree


(SortedDictionary<TKey, O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
TValue>)

Sorted Array using Binary


Search (SortedList<TKey, O(log(n)) O(?) O(1) O(1) O(log(n)) O(?) O(n) O(n) O(?)
TValue>)

Cartesian Tree ­ O(log(n)) O(log(n)) O(log(n)) ­ O(n) O(n) O(n) O(n)

Splay Tree ­ O(log(n)) O(log(n)) O(log(n)) ­ O(log(n)) O(log(n)) O(log(n)) O(n)

Red­Black Tree
(SortedSet<T> No O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Duplicates)

AVL Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)

B­Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)

Searching
Space
Algorithm Data Structure Time Complexity Complexity

Average Worst Worst

Depth First Search (DFS) Graph of |V| vertices and |E|


­ O(|E| + |V|) O(|V|)
edges

Breadth First Search (BFS) Graph of |V| vertices and |E|


­ O(|E| + |V|) O(|V|)
edges

Binary search (Array.BinarySearch or


Sorted array of n elements O(log(n)) O(log(n)) O(1)
List<T>.BinarySearch)
Linear (Brute Force) Array O(n) O(n) O(1)

Shortest path by Dijkstra, Graph with |V| vertices and O((|V| + |E|) O((|V| + |E|)
O(|V|)
using a Min­heap as priority queue |E| edges log |V|) log |V|)

Shortest path by Dijkstra, Graph with |V| vertices and


O(|V|^2) O(|V|^2) O(|V|)
using an unsorted array as priority queue |E| edges

Shortest path by Bellman­Ford Graph with |V| vertices and


O(|V||E|) O(|V||E|) O(|V|)
|E| edges

Sorting
Data Worst Case Auxiliary
Algorithm Structure Time Complexity Space Complexity

Best Average Worst Worst

Quicksort (Array.Sort and List<T>.Sort and O(n O(n


Array O(n^2) O(n)
Enumerable.OrderBy<TSource, TKey>) log(n)) log(n))

Mergesort O(n O(n O(n


Array O(n)
log(n)) log(n)) log(n))

Heapsort O(n O(n O(n


Array O(1)
log(n)) log(n)) log(n))

Bubble Sort Array O(n) O(n^2) O(n^2) O(1)

Insertion Sort Array O(n) O(n^2) O(n^2) O(1)

Select Sort Array O(n^2) O(n^2) O(n^2) O(1)

Bucket Sort Array O(n+k) O(n+k) O(n^2) O(nk)

Radix Sort Array O(nk) O(nk) O(nk) O(n+k)

Heaps
Heaps Time Complexity

Heapify Find Max Extract Max Increase Key Insert Delete Merge

Linked List (sorted) ­ O(1) O(1) O(n) O(n) O(1) O(m+n)

Linked List (unsorted) ­ O(n) O(n) O(1) O(1) O(1) O(1)

Binary Heap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)

Binomial Heap ­ O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n))

Fibonacci Heap ­ O(1) O(log(n))* O(1)* O(1) O(log(n))* O(1)

Graphs
Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query

Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)

Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)

Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)

Incidence matrix O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|E|)
Big­O Complexity Chart

You might also like