CleverSphere

Algorithms formula sheet

Every rule and formula of the trainer on one page, for revision before the exam or to print.

Practise with problems

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: 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
  • ·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
  • ·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
  • ·h(k) = k mod m
  • ·Collision: next free slot
  • ·Wrap around at the end
  • ·Start with distance 0
  • ·Always pick the open vertex with the smallest distance
  • ·Relax its neighbours
  • ·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)