Examples
45 programs, each in Python, C++ and JavaScript. Pick one to watch it run step by step.
Basics5
- Sum with a for loopAdds numbers in a for loop: one variable changing step by stepArrayPyC++JS
- Grades (if / elif / else)Picks a grade with if / else: which branch runs, and whyDecision treePyC++JS
- Nested loopsA loop inside a loop: every pair the two counters makeArrayPyC++JS
- Reading inputReads numbers from stdin, then works with themMemoryPyC++JS
- Bank account (menu)A menu loop: each choice runs a function that changes the shared accountStateDecision treePyC++JS
Recursion5
- Factorialn! by calling itself: the call stack grows, then unwindsRecursion treePyC++JS
- Fibonacci (naive)Fibonacci the slow way: the same calls again and againRecursion treePyC++JS
- Fibonacci (memo)Fibonacci with a memo: each value computed once, then looked upRecursion treeIn usePyC++JS
- Towers of HanoiTowers of Hanoi: three disks moved by two smaller problemsRecursion treeIn usePyC++JS
- Permutations (backtracking)Every ordering of a, b, c by choosing and un-choosing (backtracking)Recursion treeArrayPyC++JS
Sorting5
- Bubble sortSwaps neighbours until the largest values bubble to the endArrayPyC++JS
- Insertion sortSlides each value left into its place in the sorted partArrayPyC++JS
- Merge sortSplits the array in halves, then merges them back in orderDivide & mergeArrayPyC++JS
- QuicksortPartitions around a pivot, then sorts each sideDivide & conquerArrayPyC++JS
- Heap sortBuilds a heap, then keeps moving the largest to the endHeapRecursion treePyC++JS
Searching4
- Binary searchHalves the search range each step: lo, mid, hiArrayPyC++JS
- Two-sum with dictFinds two numbers that add up to a target with a lookup tableArrayPyC++JS
- Sliding window maximumThe largest value in each window, kept in a dequeArrayPyC++JS
- Next greater elementThe next greater element for each value, with a stackArrayPyC++JS
Linked lists4
- Linked listBuilds a linked list node by node, then walks itLinked listPyC++JS
- Reverse a linked listReverses a linked list by turning each arrow aroundLinked listPyC++JS
- Circular linked listThe last node links back to the first: a list with no endLinked listPyC++JS
- LRU cacheAn LRU cache: a map plus a doubly linked listLinked listRecursion treePyC++JS
Queues & hash tables3
- Queue and dequeFirst in, first out: items join at the rear and leave from the front; a deque works at both endsIn usePyC++JS
- Circular queueA queue in a fixed array: front and rear wrap round to the startArrayPyC++JS
- Hash tableEach key goes to the bucket its hash picks; keys that collide share a bucketHash tablePyC++JS
Trees4
- Binary treeInserts into a binary search tree, then walks it in orderBinary treeRecursion treePyC++JS
- Huffman treeHuffman coding: a priority queue merges the two rarest nodesBinary treePyC++JS
- TrieA trie of words, one letter per levelTriePyC++JS
- Red-black treeA binary search tree that keeps itself balanced by colouring nodes and rotatingBinary treeRecursion treePyC++JS
Graphs6
- BFS on graphBreadth-first search: a queue visits the graph layer by layerGraphPyC++JS
- DFS on graphDepth-first search: recursion goes as deep as it can firstGraphRecursion treePyC++JS
- BFS on a gridShortest path on a grid, spreading out from the startGridArrayPyC++JS
- DijkstraShortest paths with weights, using a priority queueGraphPyC++JS
- Topological sortAn order that respects every arrow (Kahn's algorithm)GraphPyC++JS
- Union-findUnion-find: groups merge, find follows parents to the rootForestArrayPyC++JS
Dynamic programming3
Strings3
Classes & objects3
More examples coming soon