Sorting Algorithm Visualizer

Start with [5, 2, 4, 1, 6, 3]. Open any example to follow the highlighted line of code alongside the array. Each run starts with these six numbers, so you can compare what the algorithms do first.

Bubble sort

This lesson sorts from largest to smallest. Watch the first pass: whenever a larger number is on the right, the neighbors swap. By the end of the pass, 1 has reached the right edge.

Watch a bubble sort pass

Insertion sort

Start with 5. The next value, 2, belongs before it. Watch how the algorithm makes room for 2, then inserts 4 between them. The sorted part grows one value at a time.

Follow the insertions

Merge sort

Each number starts as a sorted group of one. The first pass produces [2, 5], [1, 4] and [3, 6]. Watch the next pass merge the first two pairs by copying the smaller front value into the buffer.

Step through the merges

Quicksort

The last value, 3, is the first pivot. Follow the scan as it moves 2 and 1 to the left of the partition. Once 3 reaches its place, the two sides can be sorted separately.

Watch the first partition