Algorithms formula sheet
Every rule and formula of the trainer on one page, for revision before the exam or to print.
Algorithms formula sheet · CleverSphere
Simple sorts
Topic page →- ·Bubble sort: swap neighbours, the largest moves to the end
- ·Selection sort: find the minimum, swap once
- ·Insertion sort: insert the next element into the sorted part
Binary search
Topic page →- ·mid = ⌊(left + right) / 2⌋
- ·Smaller: right = mid − 1
- ·Larger: left = mid + 1
BFS & DFS
Topic page →- ·BFS: queue, level by level
- ·DFS: stack or recursion, depth first
- ·Mark visited vertices
Running times
Topic page →- ·O(log n): halving
- ·O(n log n): good sorting
- ·O(n²): nested loops
Quicksort
Topic page →- ·Pivot: last element
- ·i marks the end of the small part
- ·Finally swap the pivot to position i
Merge sort
Topic page →- ·Take the smaller element
- ·If one list is empty, append the rest
- ·At most n − 1 comparisons
Heap
Topic page →- ·Build-heap: sift down from the middle backwards
- ·Remove: move the last element to the front, sift down
- ·Insert: append, sift up
Binary search tree
Topic page →- ·Left smaller, right larger
- ·Preorder: root, left, right
- ·Inorder gives the sorted sequence
Hashing
Topic page →- ·h(k) = k mod m
- ·Collision: next free slot
- ·Wrap around at the end
Dijkstra
Topic page →- ·Start with distance 0
- ·Always pick the open vertex with the smallest distance
- ·Relax its neighbours
AVL tree
Topic page →- ·Balance = height left − height right
- ·Left-left: right rotation; right-right: left rotation
- ·Left-right and right-left: double rotation
Master theorem
Topic page →- ·T(n) = a · T(n/b) + n^k
- ·k < log_b(a): Θ(n^log_b a)
- ·k = log_b(a): Θ(n^k log n)
- ·k > log_b(a): Θ(n^k)
www.cleversphere.ch/tools/algorithms-trainer
