Algorithms

Sorting Algorithms

Run four classic sorting algorithms side by side. Change the array size, slow down the animation, and compare how many comparisons and swaps each algorithm needs.

6 minLevel 1/3The interactive stage stays ad-free.
Comparisons0Moves0Array size22

Quick sort: Ready. Press Play or Step to begin.

1 / 107

Yellow bars are being compared. Green means the array is fully sorted. Try the same array with bubble sort, then merge sort, and compare the number of moves.

What you are seeing

1

The bars represent values in an array. A highlighted pair shows the two elements currently being compared.

2

Bubble and insertion sort move elements locally, one comparison at a time.

3

Quick sort partitions around a pivot, while merge sort splits the array and merges sorted halves.

Why it matters

Sorting is the canonical example of algorithmic thinking. The difference between O(n²) and O(n log n) is the difference between a slow app and a responsive one at scale.

Complexity & details

Bubble/insertion: O(n²) average. Quick: O(n log n) average, O(n²) worst. Merge: O(n log n) guaranteed, O(n) extra space.

Frequently asked questions

Which sorting algorithm is fastest?+

It depends on the data and constraints. Quick sort is fast in practice, merge sort gives predictable O(n log n), and insertion sort is excellent for small or nearly sorted arrays.

Why does quick sort have a worst case?+

If the pivot is consistently the smallest or largest element, the partitions are unbalanced and the recursion becomes O(n²). Random or median pivots reduce this risk.