SORTING CINEMA / ALGORITHMS
21 sorting algorithms, explained and compared
All 21 sorting algorithms you can visualise in Sorting Cinema, grouped by category. Each page explains how the algorithm works, its complexity, a trace on a small array and an FAQ — and lets you run it in the app.
Comparison sorts
- Bubble Sort — Bubble sort repeatedly compares two neighbouring elements and swaps them when they are out of order. After each pass, the largest remaining value has moved to the right end.
- Selection Sort — Selection sort finds the minimum of the unsorted part and swaps it with the first unsorted position, repeating until done. Values become fixed from left to right.
- Insertion Sort — Insertion sort takes the next element and inserts it into the correct place within the sorted part on its left, like arranging playing cards in your hand.
- Merge Sort — Merge sort splits the array in half repeatedly and then merges the sorted halves back together until the whole array is sorted.
- Quick Sort — Quick sort picks a pivot, moves smaller elements to its left and the rest to its right, and then sorts each side recursively.
- Heap Sort — Heap sort rearranges the array into a max-heap (a binary tree where each parent is at least as large as its children) and repeatedly moves the maximum at the root to the end.
- Shell Sort — Shell sort runs insertion sort on elements a fixed distance (gap) apart, shrinks the gap, and finishes with an ordinary insertion sort.
- Cocktail Sort — Cocktail sort (cocktail shaker sort) runs bubble-sort passes alternately left-to-right and right-to-left, carrying large values to the right end and small values to the left end.
- Gnome Sort — Gnome sort moves a single position back and forth: if the element is smaller than its left neighbour it swaps and steps back, otherwise it steps forward.
- Comb Sort — Comb sort is bubble sort with a wide comparison gap that is divided by about 1.3 each pass. It finishes with gap 1 passes until no swaps occur.
- Tim Sort — Tim sort combines insertion sort and merge sort. This app visualises a simplified version: it insertion-sorts fixed runs of 8 elements and then merges the runs.
Non-comparison sorts
- Counting Sort — Counting sort counts how many times each value appears and writes the values back from smallest to largest using those counts. It never compares elements with each other.
- Radix Sort — Radix sort reorders numbers digit by digit. This app processes base-10 digits starting from the least significant (ones) digit.
- Bucket Sort — Bucket sort distributes elements into several buckets by value, sorts each bucket, and concatenates the buckets in order.
Parallel-friendly structures
- Odd-Even Sort — Odd-even sort (odd-even transposition sort) alternately compares and swaps odd-indexed and even-indexed pairs. Pairs in the same phase never overlap, which makes the structure suitable for parallel processing.
- Bitonic Sort — Bitonic sort builds a bitonic sequence — an ascending run joined to a descending run — and then sorts it by comparing and swapping halves. Its comparison pattern is fixed regardless of input, which suits parallel processing.
Unusual structures
- Pancake Sort — Pancake sort orders the array using only one operation: reversing the prefix from the front up to position k — like flipping a stack of pancakes with a spatula.
- Stooge Sort — Stooge sort recursively sorts the first two-thirds, the last two-thirds, and the first two-thirds again — a deliberately redundant procedure.
Joke sorts
- Bogo Sort — Bogo sort is a joke algorithm: check whether the array is sorted, and if not, shuffle the whole array randomly — repeat.
- Sleep Sort — Sleep sort is a joke algorithm that waits for a time proportional to each value before outputting it, so smaller values (shorter waits) come out first.
- Slow Sort — Slow sort is a joke algorithm that splits the range in half like merge sort but is deliberately designed to take the longest possible route.
Complexity and stability of all 21
| Algorithm | Best | Average | Worst | Extra space | Stability |
|---|---|---|---|---|---|
| Bubble Sort | O(n²) | O(n²) | O(n²) | O(1) | Stable |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | Usually unstable |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Stable |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Can be implemented stably (this app takes the left value on ties) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) average, O(n) worst | Usually unstable |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | Usually unstable |
| Shell Sort | O(n log n) | — | O(n²) | O(1) | Usually unstable |
| Cocktail Sort | O(n) | O(n²) | O(n²) | O(1) | Stable |
| Gnome Sort | O(n) | O(n²) | O(n²) | O(1) | Stable |
| Comb Sort | O(n log n) | — | O(n²) | O(1) | Usually unstable |
| Tim Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Stable |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | — (values are rewritten from counts, so original element order is not tracked) |
| Radix Sort | O(d(n + b)) | O(d(n + b)) | O(d(n + b)) | O(n + b) | Stable (each digit pass is stable) |
| Bucket Sort | — | — | — | O(n + number of buckets) | — (depends on how each bucket is sorted) |
| Odd-Even Sort | O(n) | O(n²) | O(n²) | O(1) | Stable |
| Bitonic Sort | O(n log² n) | O(n log² n) | O(n log² n) | O(log n) (recursion stack, excluding padding to a power of two) | Usually unstable |
| Pancake Sort | O(n²) | O(n²) | O(n²) | O(1) | Usually unstable |
| Stooge Sort | O(n^2.71) | O(n^2.71) | O(n^2.71) | O(log n) | Usually unstable |
| Bogo Sort | O(n) | O(n·n!) | Unbounded | O(1) | Unstable |
| Sleep Sort | — | — | — | — | — |
| Slow Sort | — | — | — | O(n) | — |
For how to use the app and a comparison of the main sorts, see the learning guide. Open the visualizer from the app.