DemoCode
Sorting Algorithms Visualizer demo

This is an interactive sorting visualizer that covers six algorithms: bubble, insertion, selection, merge, quick, and heap sort. You can change the array size and the animation speed, and watch each algorithm work through the same kind of unsorted array.

The goal of the page is to show not just that an array ends up sorted, but how each algorithm gets there. Each one has its own strategy, so the app offers three views of the same run.

How it works

Every algorithm is written as a JavaScript generator that yields after each comparison or swap. Each yield carries the current array, which elements to highlight, a status message, and the pseudocode line being executed. A timer pulls one step at a time, so the speed slider only changes the delay between steps. Comparison and swap (or write) counters update as the run progresses.

The six algorithms covered are:

  • Bubble sort: compares adjacent elements and swaps them when out of order, so large values drift to the end each pass.
  • Selection sort: finds the minimum of the unsorted region and places it at the front. It does many comparisons but few swaps.
  • Insertion sort: grows a sorted region by inserting each element into its place.
  • Merge sort: splits the array recursively, then merges the sorted halves.
  • Quick sort: picks a pivot, partitions around it, and recurses on each side.
  • Heap sort: builds a max-heap, then repeatedly extracts the largest element.

The three views are Bar View (the array as colored bars: comparing, swapping, selected minimum, pivot, sorted), Algorithm View (the structure behind the algorithm), and Code View (the pseudocode with the current line highlighted). The Algorithm View is different for each family: a pass history grid for bubble and selection sort, a sorted and unsorted rail for insertion sort, a live call tree for merge and quick sort (with pivot values for quick sort), and the binary heap drawn as a tree for heap sort.

Choosing an algorithm also shows a short description and a table of best, average, and worst-case time and space:

AlgorithmBestAverageWorstSpace
BubbleO(n)O(n²)O(n²)O(1)
SelectionO(n²)O(n²)O(n²)O(1)
InsertionO(n)O(n²)O(n²)O(1)
MergeO(n log n)O(n log n)O(n log n)O(n)
QuickO(n log n)O(n log n)O(n²)O(log n)
HeapO(n log n)O(n log n)O(n log n)O(1)

Controls

  • Algorithm buttons (Bubble, Selection, Insertion, Merge, Quick, Heap) start a run.
  • New Array shuffles a fresh array, and Reset stops the current run.
  • The Array Size slider runs from 5 to 55 elements (18 by default).
  • The Speed slider sets the delay between steps, from Very Slow to Turbo.
  • The Bar View, Algorithm View, and Code View tabs switch between the three views.

Built with

A single HTML page with plain JavaScript, using the DOM for the bar view and canvas for the algorithm view. There are no libraries.

The source is on GitHub.