DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Watching Algorithms Think: Why Sorting Visualizations Teach What Textbooks Can't

You can read about bubble sort's O(n^2) time complexity in a textbook. You can memorize that quicksort averages O(n log n). But until you watch bubble sort slowly pushing elements to the right while quicksort tears through the same dataset in a fraction of the comparisons, the performance difference remains abstract.

Why visualization works

Sorting algorithms are inherently visual. An array of integers maps to a bar chart. Each comparison or swap is a discrete, observable event. The pattern of comparisons reveals the algorithm's strategy in a way that pseudocode cannot.

Bubble sort's visualization shows a wave of comparisons sweeping left to right, pushing the largest unsorted element to its final position on each pass. You can see why it's O(n^2): each pass does n comparisons, and you need n passes.

Quicksort's visualization shows a partition operation splitting the array around a pivot, then recursively sorting each half. The divide-and-conquer structure is visible: the array fractures into smaller and smaller sections, each sorted independently.

Merge sort shows a bottom-up construction: individual elements merge into pairs, pairs into quads, quads into octets. The log(n) levels of merging are visible as the sorted runs grow exponentially.

The algorithms worth visualizing

Bubble Sort: The canonical bad algorithm. O(n^2) average and worst case. Useful as a teaching tool because the pattern is so clear.

Selection Sort: Finds the minimum element and places it at the front. O(n^2) but makes fewer swaps than bubble sort. The visualization shows a scan that shrinks by one element each pass.

Insertion Sort: Builds the sorted array one element at a time by inserting each element into its correct position. O(n^2) worst case but O(n) best case (already sorted input). Efficient for small or nearly-sorted arrays.

Merge Sort: Guaranteed O(n log n). The visualization clearly shows the recursive division and the merge step. Requires O(n) extra space.

Quicksort: Average O(n log n), worst case O(n^2) (rare with good pivot selection). The partition step is the most visually interesting: elements rearranging around the pivot.

Heap Sort: Builds a max-heap, then extracts elements one by one. O(n log n) guaranteed with O(1) extra space. The heap-building phase is visually distinctive.

Implementation for the browser

The core challenge in visualizing sorting is timing. The actual sort takes microseconds. You need to slow it down to human speed by inserting delays between operations.

async function bubbleSort(arr, draw) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
      await draw(arr, j, j + 1); // Visualize and delay
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The draw function renders the current array state, highlights the compared elements, and returns a promise that resolves after a delay. The delay controls visualization speed.

I built a sorting visualizer at zovo.one/free-tools/sorting-visualizer that animates all major sorting algorithms side by side, with adjustable speed, array size, and input patterns (random, sorted, reverse, nearly sorted). Seeing the algorithms race on the same data makes their performance differences tangible.

I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)