How to Learn Algorithms by Watching Them Step by Step
Use step-by-step animations to understand sorting, graph search, Dijkstra, binary search trees and dynamic programming, and connect each step to the code.

How to Learn Algorithms by Watching Them Step by Step
Reading the description of quicksort or Dijkstra's algorithm is one thing; seeing the data change at each step is another. Visual, step-by-step tracing is how many people finally understand why an algorithm works, where it is slow and what an exam question is really asking. This guide suggests a way to study with the free algorithm and data structure visualizer, which records every step on your own input and highlights the matching line of pseudo-code.
Trace by hand, then check
The most effective routine is simple. Pick a small input, predict the next three steps on paper, then press Next and compare. When your prediction is wrong, step back with the left arrow and read the highlighted line again. This active guessing builds the mental model much faster than watching an animation passively.
Sorting: count the work
Run the same twelve numbers through bubble, insertion, selection, merge, quick and heap sort and note the comparison and write counters at the end. Then switch to the Reversed and Nearly sorted inputs. Insertion sort is almost free on nearly sorted data but slow on reversed data; quicksort with the last element as pivot degrades on already sorted input; merge sort does about the same work on every input. Counting sort avoids comparisons altogether but only works for small integer ranges. These differences are exactly what big-O notation summarises, and seeing the counters change makes the notation concrete.
Watch the colours too. In quicksort the purple pivot and the blue range show the partition; in heapsort the first phase builds the heap and the second repeatedly moves the largest value to the end.
Graphs: frontier and visited set
Breadth-first and depth-first search differ only in the data structure that holds the frontier: a queue for BFS, a stack for DFS. Run both from the same start node and compare the visiting order. BFS explores in rings of equal distance, which is why it finds shortest paths in unweighted graphs; DFS dives deep first, which suits cycle detection and topological ordering.
Dijkstra's algorithm is BFS with a priority: always finalise the unvisited node with the smallest known distance. Follow the distance labels as they shrink when a shorter path is found. Add a negative edge and the visualizer warns that the result may be wrong, a classic exam point. Prim's algorithm looks similar but grows a minimum spanning tree by always taking the cheapest edge leaving the tree. Kahn's topological sort repeatedly outputs a node with no remaining incoming edges and reports a cycle if it gets stuck.
Trees: shape depends on order
Insert the same values in a different order and the binary search tree changes shape. Sorted input produces a long chain, which is why balanced trees exist. Search follows one path from root to leaf, so its cost is the height. Deleting a node with two children copies its in-order successor, a step many students forget. Traversals show the same tree read in four orders; in-order of a search tree is always sorted.
Dynamic programming: tables and dependencies
Dynamic programming feels abstract until you see the table fill. In the longest common subsequence and edit distance problems, each cell depends on the cell above, to the left and diagonally; the visualizer shades those dependencies in blue while the orange cell is computed. In the knapsack table each row adds one item and each column one unit of capacity. After the last cell, the green trace-back path shows how the answer is reconstructed, which is often the part asked in interviews.
Use it for interview practice
Choose a problem, write the code yourself, then use the visualizer to check intermediate states, not just the final answer. Bugs in DP and graph code usually show up as one wrong cell or one node visited too early.
Limits
Inputs are kept small so the pictures stay readable, and the animations show logical steps rather than real running time on a computer. The pseudo-code follows textbook conventions, so details such as pivot choice or tie-breaking may differ from the version in your course; check your course material for the exact variant you are tested on.



