Sorting algorithms, visualized
Sorting is where Big-O stops being abstract: the same input takes visibly different numbers of steps. Step through each one on your own array.
Bubble and selection sort show the idea; merge and quick sort are what interviews ask about; counting and radix sort show how to beat comparison sorting.
Start with Bubble Sort 7 topics
Simple comparison sorts
Quadratic, but the clearest way to see what sorting does.
- Bubble SortThe simplest sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. "Bubbling" the largest element to the top.Beginner10m
- Selection SortA simple comparison-based sorting algorithm. It divides the list into sorted and unsorted regions, repeatedly selecting the minimum element from the unsorted region.Beginner10m
Divide and conquer
The O(n log n) sorts used in practice.
- Merge SortA classic Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.Intermediate15m
- Quick SortA highly efficient Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot.Intermediate20m
- Heap SortA comparison-based sorting algorithm that uses a Binary Heap data structure. It divides its input into a sorted and an unsorted region, and iteratively shrinks the unsorted region by extracting the largest element.Intermediate20m
Non-comparison sorts
Linear time when the keys allow it.
- Counting SortA non-comparison sorting algorithm that achieves O(n+k) time by counting occurrences. Perfect when the range of values (k) is not significantly larger than n.Intermediate20m
- Radix SortSort integers digit by digit using a stable sort (like Counting Sort) as a subroutine. Achieves O(d×(n+b)) time where d is the number of digits and b is the base.Intermediate25m