← 🤖 Algorithms

📊 Sorting

Algorithm
Comparisons0
Swaps 0
Array accesses0
Time
Status Ready
Unsorted
Comparing
Swapping
Pivot / key
Sorted
Select algorithm · Resize array · Click Sort · Toggle sound

📊 Sorting Algorithms — Visual & Audio

12 sorting algorithms animated as bar charts with Web Audio tones. Compare speed, comparisons and swap counts across Bubble Sort, Quick Sort, Merge Sort, Heap Sort and many more.

🔬 What It Demonstrates

Each algorithm rearranges bars in real time. Comparisons are highlighted, swaps are animated, and audio pitch maps bar height — you can literally hear the sorting process.

🎮 How to Use

Select an algorithm and array size. Click Sort and watch (and listen). Compare different algorithms on the same data to see the speed difference.

💡 Did You Know?

Quick Sort averages O(n log n) but can degrade to O(n²). Merge Sort is always O(n log n) but needs extra memory. No comparison-based sort can beat O(n log n) on average — this was proven in 1972.

About the Sorting Algorithms Visualiser

This simulation animates twelve classic sorting algorithms as a row of vertical bars whose heights represent the values being ordered. As each algorithm runs, bars being compared, swapped, overwritten or marked as a pivot light up in distinct colours, while a Web Audio oscillator maps each bar's height to a pitch between 120 Hz and 1600 Hz — so you can literally hear the array becoming sorted. Live counters track comparisons, swaps, array accesses and elapsed time, letting you measure the real cost of each method.

Sorting is one of the most studied problems in computer science because almost every larger program relies on ordered data — for binary search, deduplication, database indexing, rendering and more. The algorithms shown span the major design families: simple quadratic methods (bubble, insertion, selection), divide-and-conquer methods (merge, quick), heap-based selection (heap sort), and non-comparison integer sorts (counting, radix). A foundational 1972 result proved that no comparison-based sort can beat O(n log n) comparisons on average, which is exactly why merge, quick and heap sort dominate general-purpose libraries.

Frequently Asked Questions

What do the colours of the bars mean?

Blue bars are unsorted, orange marks elements currently being compared, red marks a swap in progress, purple is a pivot or key element, cyan is an overwrite (used by merge, counting and radix sort), and green bars are confirmed to be in their final sorted position.

Why can I hear the sorting?

Each time the algorithm touches a bar, a short triangle-wave tone is played whose frequency is mapped logarithmically to that bar's value. Low bars sound deep and tall bars sound high, so a sorted array produces a smooth rising scale — an audible signature of order emerging from chaos.

Which algorithm is the fastest?

For random data, quick sort, merge sort and heap sort (all averaging O(n log n)) are far faster than the O(n²) methods like bubble or selection sort. For integer keys in a small range, the non-comparison sorts (counting at O(n+k), radix at O(nk)) can be faster still, since they sidestep the O(n log n) comparison lower bound.

Why is Quick Sort sometimes slow?

Quick sort averages O(n log n) but degrades to O(n²) in the worst case — typically when the pivot repeatedly turns out to be the smallest or largest element, which can happen on already-sorted or adversarial input. Real implementations mitigate this with randomised or median-of-three pivot selection.

What makes Merge Sort different from Quick Sort?

Merge sort always runs in O(n log n) time regardless of input and is a stable sort, but it needs O(n) extra memory to merge sub-arrays. Quick sort sorts in place with less memory and is usually faster in practice due to better cache behaviour, but it is not stable and has that O(n²) worst case.

How can Counting and Radix Sort beat O(n log n)?

They are not comparison-based. Counting sort tallies how many times each value occurs and reconstructs the array directly, running in O(n+k) where k is the value range. Radix sort processes numbers digit by digit using a stable bucket pass. Because they never compare two elements against each other, the comparison lower bound does not apply.

What is a stable sort and why does it matter?

A stable sort preserves the relative order of elements that compare equal. This matters when sorting records by multiple keys — for example, sorting by name and then stably by date keeps same-date entries alphabetised. Merge, insertion and counting sort are stable; quick, heap and selection sort generally are not.

Why include "slow" algorithms like Bubble Sort at all?

Quadratic sorts like bubble, gnome and cocktail-shaker sort are rarely used in production, but they are excellent teaching tools: their simple logic makes the comparison-and-swap mechanic obvious, and watching them crawl through a large array gives an intuitive feel for why algorithmic complexity matters.

Does array size affect which algorithm wins?

Yes. On very small arrays the constant-factor overhead of clever algorithms can make simple methods like insertion sort competitive — which is why many real libraries switch to insertion sort below a threshold. As the array grows, the O(n log n) methods pull decisively ahead, and the gap widens dramatically.

Are these the same algorithms used in real software?

Yes, the core ideas are identical. Most language standard libraries use hybrid sorts — for instance Timsort (a merge/insertion hybrid) in Python and Java's object sort, and introsort (quick sort that falls back to heap sort) in C++. This visualiser shows the textbook building blocks those production sorts are assembled from.