Sorting Visualizer
Listen to the data. Watch how different algorithms tackle the chaos of randomness.
How Sorting Algorithms Work
Sorting is one of the most fundamental problems in computer science. Arranging data into a meaningful order underpins nearly every computational task — from database queries to rendering search results. This visualiser lets you watch each algorithm's strategy in real time: how elements are compared, swapped, and gradually brought into order.
Audio maps each element's value to a pitch, creating a multi-sensory experience that reveals the inner logic of each algorithm. Low-pitched tones represent small values; high-pitched tones represent large values. As the array approaches sorted order, the audio sweeps smoothly from low to high.
Algorithm Comparison
Bubble Sort — O(n²)
The simplest sorting algorithm. Repeatedly compares adjacent pairs and swaps them if they are in the wrong order. “Large” elements bubble up to the end like bubbles in water. Easy to understand but very slow on large datasets.
Selection Sort — O(n²)
Finds the minimum element in the unsorted portion and places it at the beginning. Repeats for the remainder of the array. Minimises the number of swaps (O(n)) but always performs O(n²) comparisons regardless of input order.
Insertion Sort — O(n²)
Builds the sorted array one element at a time — like sorting a hand of playing cards. Each new element is inserted into its correct position among the already-sorted elements. Very efficient on nearly-sorted data.
Merge Sort — O(n log n)
A divide-and-conquer algorithm: splits the array in half, recursively sorts each half, then merges the sorted halves together. Guaranteed O(n log n) in all cases but requires O(n) additional memory. Invented by John von Neumann in 1945.
Quick Sort — O(n log n) average
Picks a pivot element, partitions the array into elements less than and greater than the pivot, then recursively sorts each partition. The fastest in practice for most datasets. Invented by Tony Hoare in 1959. Worst case is O(n²) but rare with good pivot selection.
Heap Sort — O(n log n)
Uses a binary heap data structure to repeatedly extract the maximum element. Guaranteed O(n log n) with O(1) extra memory — combining the best properties of merge sort (guaranteed performance) and quick sort (in-place operation).
Big-O Notation Explained
Big-O notation describes how an algorithm's running time grows relative to the input size. It captures the worst-case scaling behaviour, ignoring constant factors:
- O(n²) means the work roughly squares when the input doubles.
- O(n log n) means the work grows only slightly faster than linearly.
What this means in practice:
For 1,000 elements: O(n²) performs ~1,000,000 comparisons while O(n log n) performs ~10,000. That’s a 100× difference.
For 1,000,000 elements: O(n²) performs ~1012 comparisons while O(n log n) performs ~20,000,000. That’s the difference between hours and milliseconds.
Sorting in the Real World
Sorting algorithms are everywhere in modern computing:
- Database indexing — B-trees and sorted indices enable O(log n) lookups instead of O(n) full scans.
- Search engine result ranking — sorting billions of pages by relevance in milliseconds.
- Spreadsheet column sorting — every click on a column header triggers a sorting algorithm.
- Media library organisation — sorting music by artist, photos by date, files by name.
Every interaction with sorted data depends on these fundamental algorithms. Most modern programming languages use Timsort — a hybrid of merge sort and insertion sort — as their default sorting algorithm. It was designed by Tim Peters in 2002 for Python and is now used in Java, Android, Swift, and Rust.
Experiments to Try
- Compare Bubble vs Quick on large arrays — watch the dramatic O(n²) vs O(n log n) speed difference unfold in real time.
- Test on already-sorted data — some algorithms (Insertion Sort) become O(n) while others (Selection Sort) remain O(n²) regardless.
- Try reverse-sorted data — this is often the worst case for algorithms like Bubble Sort and naive Quick Sort.
- Listen to the audio patterns — each algorithm has a unique “sound signature.” Bubble Sort bubbles slowly; Quick Sort makes frantic recursive sweeps; Merge Sort produces rhythmic, symmetric merging sounds.
Share this simulation
Send this page to students or colleagues.
Related simulations
Continue with similar experiments.
đ Recommended Reading
Deepen your understanding with our in-depth articles.
Time Complexity Comparison
Sorting algorithms differ dramatically in speed. Bubble Sort and Insertion Sort have O(n²) worst-case giving thousands of operations for just 100 elements. In contrast, Merge Sort and Heap Sort guarantee O(n log n), making them reliable for large datasets regardless of input order.
Divide & Conquer Strategy
Merge Sort splits the array in half recursively, sorts each half, then merges a classic divide-and-conquer algorithm. Quick Sort selects a pivot and partitions around it. Quick Sort is typically faster in practice (O(n log n) average) but has an O(n²) worst case with bad pivot choice. Randomised pivot selection avoids this.
Space Complexity & Stability
A stable sort preserves the relative order of equal elements important when sorting records by multiple keys. Merge Sort is stable but requires O(n) extra space. Quick Sort is in-place but unstable. Heap Sort is in-place and O(n log n) but not stable. Radix Sort achieves O(nk) even beating the O(n log n) comparison-sort lower bound.
The Comparison Sort Lower Bound
Any comparison-based sorting algorithm requires at least Ω(n log n) comparisons in the worst case proven via decision-tree information theory. To sort n elements, you must distinguish n! orderings; with binary comparisons you need at least log&sub2;(n!) ≈ n log n comparisons. Merge Sort, Heap Sort, and introsort achieve this optimal bound.
Algorithm Complexity
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable? |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Radix Sort | O(nk) | O(nk) | O(nk) | O(n+k) | Yes |
Curriculum Relevance
| Level | Topic | Relevance |
|---|---|---|
| GCSE / A-Level CS | Algorithms & data structures | Bubble sort, merge sort, binary search complexity |
| IB Computer Science | Algorithm design | Sorting algorithms, Big-O notation, pseudocode |
| AP Computer Science | Algorithms | Sorting, searching, recursion, divide-and-conquer |
| Undergraduate CS | Algorithms & complexity | Formal analysis, decision trees, lower bounds, NP |
| Postgraduate | Advanced algorithms | Cache-oblivious sorting, parallel sort, external sort |
🔒 Unlock All 32 Simulations
Get unlimited access to all 32 simulations — including the Sorting Visualiser, Pathfinding, Neural Network, and more with MySimulator Premium.
View Premium Plans