Sorting algorithm visualiser
Choose an algorithm and press Play, or Step to go one operation at a time. Yellow bars are being compared, red bars are being swapped or written, purple is the pivot and green bars are in their final place. The counters show how much work each algorithm does.
Why sorting matters
Sorted data is much faster to search (with binary search), easier to read and easier to combine. Databases, spreadsheets and search engines sort all the time, so computer scientists study how much work each method needs. That work is measured by counting comparisons and swaps as the list gets longer, written in Big O notation.
The simple sorts: O(n²)
Bubble, selection and insertion sort compare items in pairs, roughly n × n times. They are easy to write and fine for short lists, but doubling the list size makes them about four times slower. Insertion sort is the best of the three in practice, and very fast when the list is already nearly in order.
The fast sorts: O(n log n)
Merge sort and quick sort use divide and conquer: split the problem into halves, solve each half, then combine. Heap sort uses a heap, a tree stored inside the array. Doubling the list makes these only a little more than twice as slow. For 1 000 000 items that is about 20 million steps instead of a trillion.
Stable and in-place
A stable sort keeps items with equal values in their original order: important when you sort a class list by surname after sorting it by first name. An in-place sort needs almost no extra memory; merge sort needs a second array as big as the first.
Each algorithm here is written as a generator that pauses at every comparison and swap, so what you see is exactly what the code does.
Questions students ask
Which sorting algorithm is the fastest?
For large lists, O(n log n) algorithms such as quick sort, merge sort and heap sort are fastest; quick sort is usually quickest in practice, while insertion sort wins on very small or nearly sorted lists.
What is the difference between bubble sort and insertion sort?
Bubble sort repeatedly swaps neighbouring pairs so the largest value moves to the end each pass, while insertion sort takes each value and slides it into place among the already sorted values; both are O(n²) but insertion sort usually does less work.
Why is quick sort slow on a sorted list?
Quick sort is slow on an already sorted list when it uses the last value as the pivot, because every split is as uneven as possible, which makes it O(n²); choosing a random or middle pivot avoids this.
Check yourself
3 quick questions. Tap an answer to see if you are right.
1. What is the average time complexity of merge sort?
Merge sort halves the list (log n levels) and merges n items on each level.
2. Which sort is very fast on a list that is already nearly sorted?
Insertion sort only moves the few items that are out of place, so it is close to O(n).
3. A stable sort…
Stability matters when you sort by one key after another.
Related
More computer science
Connected tools
The same idea from another angle, across the site.