View on GitHub

wiki

Algorithms and data structures

Definitions

Functions

Tree traversal

P/NP

Basic strategies

Big O notation

Sorting

Sort algo Best Avg Worst Notes
Hash O(n) O(n) O(n) Good if sorting something w/ frequent repeats (e.g., 1mil people, sorting by Age in years)
Bubble O(n^2) O(n^2) O(n^2) (in-place)
Insertion O(n^2) O(n^2) O(n^2) (in-place)
Selection O(n^2) O(n^2) O(n^2) (in-place)
Quick O(n*logn) O(n*logn) O(n^2) (common implementation not in-place)
Merge O(n*logn) O(n*logn) O(n*logn) (common implementation not in-place)
Heap O(n*logn) O(n*logn) O(n*logn) (in-place)

Data structures

Stack (LIFO) - list/array structure

Queue/Deque/Circular Queue (FIFO)

Binary Search Tree

Balanced Binary Search Tree

Red-black tree

B-tree

PriorityQueue

Heap

Dictionaries/map

Hash table

Graphs

Tries

Data structure performance

Header text Linked list Array Dynamic array Balanced tree Random access list
Indexing T(n) T(1) T(1) T(log n) T(log n)
Insert/delete at beginning T(1) N/A T(n) T(log n) T(1)
Insert/delete at end T(1) N/A T(1)amortized T(log n) T(log n) updating
Insert/delete in middle search + T(1) N/A T(n) T(log n) T(log n) updating
Wasted space (average) T(n) 0 T(n) T(n) T(n)
Search T(n) T(n) T(n) T(logn) T(n)

Specific solutions

Permutation

Recursive: permutation(remainingString) - given remaining string, run permutation once for each letter

def permutation(remainingString):
    if len(remainingString) == 1:
        return remainingString
    perm_list = [] # resulting list
    for i in range(len(remainingString)):
        m1String = remainingString[:i] + remainingString[i+1:]
        z = permutation(m1String) # permutations of sublist
        for t in z:
            perm_list.append(remainingString[i] + t)
    return perm_list
print permutation(strngy)

Prime

Tree’s lowest common ancestor

Recursion

Minimum spanning tree algorithms (greedy)

Shortest path algorithms