Comp Sci GCSE • A-Level • AP ●○○ Beginner ★ Free

Sorting Visualizer

Listen to the data. Watch how different algorithms tackle the chaos of randomness.

Speed: 50%
Bars: 100
Comparisons: 0 | Swaps: 0
Simulation running

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.

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

AlgorithmBest CaseAverage CaseWorst CaseSpaceStable?
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Radix SortO(nk)O(nk)O(nk)O(n+k)Yes

Curriculum Relevance

LevelTopicRelevance
GCSE / A-Level CSAlgorithms & data structuresBubble sort, merge sort, binary search complexity
IB Computer ScienceAlgorithm designSorting algorithms, Big-O notation, pseudocode
AP Computer ScienceAlgorithmsSorting, searching, recursion, divide-and-conquer
Undergraduate CSAlgorithms & complexityFormal analysis, decision trees, lower bounds, NP
PostgraduateAdvanced algorithmsCache-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