Sorting visualizer
Watch five algorithms order an array: orange bars are being compared, red bars are swapped.
Animation
Comparisons 0Writes 0
Algorithms compared
| Algorithm | Best | Average | Worst | Memory |
|---|---|---|---|---|
| Bubble sortNeighbours swap places until the largest one bubbles to the end. | O(n) | O(n²) | O(n²) | O(1) |
| Insertion sortEach new element is inserted into its place among the sorted ones. | O(n) | O(n²) | O(n²) | O(1) |
| Selection sortFind the smallest element, put it first, then repeat for the rest. | O(n²) | O(n²) | O(n²) | O(1) |
| Merge sortSplit in half, sort the halves and merge them into one sorted array. | O(n log n) | O(n log n) | O(n log n) | O(n) |
| QuicksortPick a pivot, put smaller ones left and larger ones right, then repeat. | O(n log n) | O(n log n) | O(n²) | O(log n) |
On small arrays the difference is invisible, but for a million items O(n²) means a trillion steps. Try raising the size and compare the counters.
More in the Study
Algorithms are not just theory
In the courses you write them yourself and see at once whether the code works.