algorithms, racing

Sort Race

Four sorting algorithms run on the same shuffled array and race each other to finish first. Each one is written as a step-generator, which means the algorithm hands control back to the page after every comparison or swap so the canvas can draw between steps. The slower O(n²) sorts (bubble and insertion) usually finish well after merge and quick. Bigger arrays make the gap between them more obvious.

Bubble

O(n²)
cmp 0 swap 0 idle

Insertion

O(n²)
cmp 0 swap 0 idle

Merge

O(n log n)
cmp 0 swap 0 idle

Quick

O(n log n) avg
cmp 0 swap 0 idle

Press Race to start. Highlighted bars are the indices the algorithm is currently looking at.