Big O Cheat Sheet

A quick reference of the time and space costs of all the major data structures and algorithms



What the heck even is big O notation?

Data Structures

index search insert delete
Array
index
search
insert
delete
An array of letters from a to f with numbered indices 0 through 5.

Stores things in order. Has quick lookups by index.

Full array reference

Dynamic Array
index
search
insert
delete
The dynamic array has a size of 3 and a capacity of 6.

An array that automatically grows as you add more items.

Full dynamic array reference

Linked List
index
search
insert
delete
A linked list with the letters a,b,c,d. The letter a points to b, b points to c and c points to d.

Also stores things in order. Faster insertions and deletions than arrays, but slower lookups (you have to "walk down" the whole list).

Full linked list reference

Queue
index
search
insert
delete
A queue composed of the letters a, b and c. c is the first (rightmost) item, then b and then a.

Like the line outside a busy restaurant. "First come, first served."

Full queue reference

Stack
index
search
insert
delete
A stack composed of the letters a, b and c. a is the first item and placed at the top of the stack, followed by b and then c.

Like a stack of dirty plates in the sink. The first one you take off the top is the last one you put down.

Full stack reference

Hash Table
index
search
insert
delete
An array that contains 4 keys and corresponding values: key "apple" has value 4, "cherry" has value 2, "pecan" has value 5 and "blueberry" has value 1.

Like an array, except instead of indices you can set arbitrary keys for each value.

Full hash table reference

Binary Search Tree
index
search
insert
delete
A binary search tree with nodes containing integers. The root node contains the integer 50. Each child node to the left of the root contains integers less than 50, and each child node to the right of the root contains integers greater than 50.

Everything in the left subtree is smaller than the current node, everything in the right subtree is larger. <span complexity="lgn"></span> lookups, but only if the tree is balanced!

Full binary search tree reference

Trie
index
search
insert
delete
The trie has four branches and represents four different words: "POT", "PAST", "PASS", "PART". All words are sharing parent node "P". Words "PAST", "PASS", "PART" are sharing node "A". Words "PAST and "PASS" also sharing node "S".

Stores a set of strings in a big tree of characters. Good for lookups by prefix. <em>Sometimes</em> saves space.

Full trie reference

Heap
index
search
insert
delete
A binary heap is a binary tree where the nodes are organized to so that the smallest value is always at the top.

A binary tree where the smallest value is always at the top. Use it to implement a priority queue.

Full heap reference

Priority Queue
index
search
insert
delete
An element with high priority which is equal 1 is served before elements with lower priority: 2, 28, 39, 84 and 99. So the new element with priority 2 will be served before an element with priority 28 but after the existing element with priority 2.

A queue where items are ordered by priority.

Full priority queue reference

Bloom filter
index
search
insert
delete
hash("lemon") % 8 = 2. The bit at index 2 is O, so "lemon" is DEFINITELY NOT present in the set (true negative).

A constant-space bitmap that lets you quickly check whether or not an item is in a set. Can give false positives.

Full bloom filter reference

LRU Cache
index
search
insert
delete
Doubly-linked list with a dictionary where each item contains links to both its predecessor and its successor.

Lets you quickly identify which item hasn't been used for the longest amount of time.

Full lru cache reference

Sorting Algorithms

worst best average space
Selection Sort
worst
best
average
space
The first two items in [1, 2, 7, 8, 4, 3, 6] are sorted. 3 is the smallest item in the unsorted portion ([7, 8, 4, 3, 6]). Swapping 3 with 7, we have [1, 2, 3, 7, 4, 7, 6] and the first three items are sorted.

Selection sort works by repeatedly "selecting" the next-smallest element from the unsorted array and moving it to the front.

Full selection sort reference

Insertion Sort
worst
best
average
space
The list [2, 4, 7, 8, 1, 3, 6] is partially sorted. (1) is compared with its left neighbor (8) and switches places to create [2, 4, 7, 1, 8, 3, 6]. Then (1) is compared with (7).

Insertion sort works by inserting elements from an unsorted array into a sorted subsection of the array, one item at a time.

Full insertion sort reference

Merge Sort
worst
best
average
space
Two small sorted lists ([2, 4, 7, 8] and [1, 3, 6, 9]) are merged into a large sorted list ([1, 2, 3, 4, 6, 7, 8, 9]).

Merge sort works by splitting the input in half, recursively sorting each half, and then merging the sorted halves back together.

Full merge sort reference

Quicksort
worst
best
average
space
An unsorted list [2, 7, 3, 9, 1, 6, 8, 4] is rearranged around a pivot element, 4. The resulting list is [2, 1, 3, 4, 7, 6, 8, 9]. [2, 1, 3] are less than the pivot and appear to the left; [7, 6, 8, 9] are greater than the pivot and appear to the right.

Quicksort works by recursively dividing the input into two smaller arrays around a pivot item: one half has items smaller than the pivot, the other has larger items.

Full quicksort reference

Heapsort
worst
best
average
space
An unsorted input [2, 4, 7, 8, 1, 3, 6] is transformed into a binary heap (8; 2, 7; 4, 1, 3, 6) which is used to create the sorted output [1, 2, 3, 4, 6, 7, 8].

Heapsort is similar to selection sort—we're repeatedly choosing the largest item and moving it to the end of our array. But we use a heap to get the largest item more quickly.

Full heapsort reference

Counting Sort
worst
best
average
space
The input [1, 3, 7, 8, 1, 1, 3] has three (1)'s, two (3)'s, and a (7) and (8). Encoding that in a list of counts, we have [0, 3, 2, 0, 0, 0, 1, 1, 0, 0].

Counting sort works by iterating through the input, counting the number of times each item occurs, and using those counts to compute each item's index in the final, sorted array.

Full counting sort reference

Radix Sort
worst
best
average
space
Sorting [23, 31, 81, 27, 56, 37, 51] on the ones place, we have [31, 51, 81, 23, 56, 27, 37].

Radix sort works by sorting the input numbers one digit at a time.

Full radix sort reference

. . .