Sorting Arena
Compare Bubble, Selection, Insertion, Quick, Merge, and Heap sorts in real-time.
About this tool
Sorting algorithms differ far more than their final output suggests. Some make many comparisons but few writes, some are fast on nearly sorted data and slow on random data, some need extra memory, and only some keep equal elements in their original order (stability).
This arena animates six classic algorithms — bubble, selection, insertion, quick (median-of-three pivot), merge and heap sort — on the same data and counts comparisons and writes as they go. Change the input shape to see which algorithms adapt and which do not.
How to use Sorting Arena
- Choose an algorithm and a data shape: random, nearly sorted, reversed or few unique values.
- Press play. Yellow bars are being compared, red bars are being written or swapped, green bars are in their final position.
- Read the counters and the complexity table under the chart, then switch algorithms on the same data to compare them.
- Slow the speed down or step manually to follow a single partition or merge.
Examples
Insertion sort loves nearly sorted data
30 elements, nearly sorted
Roughly n comparisons, while selection sort still makes n(n−1)/2 = 435
Quicksort pivot choice
Reversed input
Median-of-three picks a middle value, so the partitions stay balanced instead of degrading to O(n²)
Bubble sort early exit
Already sorted input
One pass with no swaps, then it stops: n−1 comparisons
Implementation in five languages
The same algorithm in Python, JavaScript, Java, Go and C++. Each program is complete and runnable; the comments show what it prints.
def quick_sort(a, lo=0, hi=None):
"""In place, Lomuto partition with the last element as pivot."""
if hi is None:
hi = len(a) - 1
if lo >= hi:
return a
pivot, i = a[hi], lo
for j in range(lo, hi):
if a[j] < pivot:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
quick_sort(a, lo, i - 1)
quick_sort(a, i + 1, hi)
return a
def merge_sort(a):
"""Returns a new sorted list; stable."""
if len(a) <= 1:
return a
mid = len(a) // 2
left, right = merge_sort(a[:mid]), merge_sort(a[mid:])
out, i, j = [], 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
out.append(left[i])
i += 1
else:
out.append(right[j])
j += 1
return out + left[i:] + right[j:]
data = [5, 2, 9, 1, 5, 6, 3]
print(quick_sort(data[:])) # [1, 2, 3, 5, 5, 6, 9]
print(merge_sort(data)) # [1, 2, 3, 5, 5, 6, 9] // In place, Lomuto partition with the last element as pivot.
function quickSort(a, lo = 0, hi = a.length - 1) {
if (lo >= hi) return a;
const pivot = a[hi];
let i = lo;
for (let j = lo; j < hi; j++) {
if (a[j] < pivot) {
[a[i], a[j]] = [a[j], a[i]];
i++;
}
}
[a[i], a[hi]] = [a[hi], a[i]];
quickSort(a, lo, i - 1);
quickSort(a, i + 1, hi);
return a;
}
// Returns a new sorted array; stable.
function mergeSort(a) {
if (a.length <= 1) return a;
const mid = a.length >> 1;
const left = mergeSort(a.slice(0, mid));
const right = mergeSort(a.slice(mid));
const out = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
out.push(left[i] <= right[j] ? left[i++] : right[j++]);
}
return out.concat(left.slice(i), right.slice(j));
}
const data = [5, 2, 9, 1, 5, 6, 3];
console.log(quickSort([...data]).join(', ')); // 1, 2, 3, 5, 5, 6, 9
console.log(mergeSort(data).join(', ')); // 1, 2, 3, 5, 5, 6, 9 import java.util.Arrays;
public class Main {
// In place, Lomuto partition with the last element as pivot.
static void quickSort(int[] a, int lo, int hi) {
if (lo >= hi) return;
int pivot = a[hi], i = lo;
for (int j = lo; j < hi; j++) {
if (a[j] < pivot) {
int t = a[i]; a[i] = a[j]; a[j] = t;
i++;
}
}
int t = a[i]; a[i] = a[hi]; a[hi] = t;
quickSort(a, lo, i - 1);
quickSort(a, i + 1, hi);
}
// Sorts a[lo, hi) using tmp as scratch space; stable.
static void mergeSort(int[] a, int[] tmp, int lo, int hi) {
if (hi - lo <= 1) return;
int mid = (lo + hi) >>> 1;
mergeSort(a, tmp, lo, mid);
mergeSort(a, tmp, mid, hi);
int i = lo, j = mid, k = lo;
while (i < mid && j < hi) tmp[k++] = a[i] <= a[j] ? a[i++] : a[j++];
while (i < mid) tmp[k++] = a[i++];
while (j < hi) tmp[k++] = a[j++];
System.arraycopy(tmp, lo, a, lo, hi - lo);
}
public static void main(String[] args) {
int[] data = {5, 2, 9, 1, 5, 6, 3};
int[] q = data.clone();
quickSort(q, 0, q.length - 1);
System.out.println(Arrays.toString(q)); // [1, 2, 3, 5, 5, 6, 9]
int[] m = data.clone();
mergeSort(m, new int[m.length], 0, m.length);
System.out.println(Arrays.toString(m)); // [1, 2, 3, 5, 5, 6, 9]
}
} package main
import "fmt"
// quickSort sorts in place: Lomuto partition with the last element as pivot.
func quickSort(a []int) {
if len(a) <= 1 {
return
}
hi := len(a) - 1
pivot, i := a[hi], 0
for j := 0; j < hi; j++ {
if a[j] < pivot {
a[i], a[j] = a[j], a[i]
i++
}
}
a[i], a[hi] = a[hi], a[i]
quickSort(a[:i])
quickSort(a[i+1:])
}
// mergeSort returns a new sorted slice; stable.
func mergeSort(a []int) []int {
if len(a) <= 1 {
return a
}
mid := len(a) / 2
left, right := mergeSort(a[:mid]), mergeSort(a[mid:])
out := make([]int, 0, len(a))
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] <= right[j] {
out = append(out, left[i])
i++
} else {
out = append(out, right[j])
j++
}
}
out = append(out, left[i:]...)
return append(out, right[j:]...)
}
func main() {
data := []int{5, 2, 9, 1, 5, 6, 3}
q := append([]int(nil), data...)
quickSort(q)
fmt.Println(q) // [1 2 3 5 5 6 9]
fmt.Println(mergeSort(data)) // [1 2 3 5 5 6 9]
} #include <iostream>
#include <utility>
#include <vector>
// In place, Lomuto partition with the last element as pivot.
void quickSort(std::vector<int>& a, int lo, int hi) {
if (lo >= hi) return;
int pivot = a[hi], i = lo;
for (int j = lo; j < hi; j++) {
if (a[j] < pivot) std::swap(a[i++], a[j]);
}
std::swap(a[i], a[hi]);
quickSort(a, lo, i - 1);
quickSort(a, i + 1, hi);
}
// Sorts a[lo, hi) using tmp as scratch space; stable.
void mergeSort(std::vector<int>& a, std::vector<int>& tmp, size_t lo, size_t hi) {
if (hi - lo <= 1) return;
size_t mid = lo + (hi - lo) / 2;
mergeSort(a, tmp, lo, mid);
mergeSort(a, tmp, mid, hi);
size_t i = lo, j = mid, k = lo;
while (i < mid && j < hi) tmp[k++] = a[i] <= a[j] ? a[i++] : a[j++];
while (i < mid) tmp[k++] = a[i++];
while (j < hi) tmp[k++] = a[j++];
for (k = lo; k < hi; k++) a[k] = tmp[k];
}
void print(const std::vector<int>& a) {
for (int x : a) std::cout << x << ' ';
std::cout << '\n';
}
int main() {
const std::vector<int> data = {5, 2, 9, 1, 5, 6, 3};
std::vector<int> q = data;
quickSort(q, 0, static_cast<int>(q.size()) - 1);
print(q); // 1 2 3 5 5 6 9
std::vector<int> m = data, tmp(m.size());
mergeSort(m, tmp, 0, m.size());
print(m); // 1 2 3 5 5 6 9
} Practice on LeetCode
Classic problems that use this algorithm. Try them after working through the lab. Links open on LeetCode.
At a glance
- Bubble, insertion: O(n²) average, O(n) best, stable, in place.
- Selection: O(n²) always, at most n−1 swaps, not stable.
- Quick: O(n log n) average, O(n²) worst, in place, not stable; usually the fastest in practice.
- Merge: O(n log n) always, stable, needs O(n) extra memory.
- Heap: O(n log n) always, in place, not stable.
What real libraries do
Production sorts are hybrids. Python and Java (for objects) use Timsort, a merge sort that detects existing runs and uses insertion sort for short ones. C++ std::sort uses introsort: quicksort that switches to heapsort if recursion gets too deep and to insertion sort for small ranges.
Frequently asked questions
Everything you need to know about this tool and data security.
What does stable mean and why does it matter?
A stable sort keeps equal keys in their input order. It lets you sort by one column and then another (e.g. by city, then by name) without losing the first ordering.
Why count writes separately?
On some media (flash, EEPROM) or with large records, writes cost much more than comparisons. Selection sort makes few writes even though it compares a lot.
Can any comparison sort beat O(n log n)?
Not in the worst case: there are n! orderings and each comparison halves the possibilities at best. Non-comparison sorts (counting, radix) can do better for integers or fixed-length keys.
Is my data safe when using this tool?
Yes, 100% safe. This tool operates entirely inside your web browser using client-side JavaScript and WebAssembly. Your data never touches any external server.
Related Tools
Interactive Max/Min Heap with array vs tree dual view and sift animations.
Interactive graph sandbox visualizing Queue-based BFS and Call Stack DFS.
Interactive BST visualizer with insertion, deletion, and tree traversals.
Featured Projects tech-fun.net
Explore more AI & productivity tools
Nihongo GameBox
Master Japanese typing, Kana, and JLPT N5–N1 vocabulary through interactive retro arcade games.
ResumeGPT
Tailors CVs and cover letters to every job opportunity with a multi-agent LLM pipeline, job tracking, and scheduled discovery.
CollectMind
Turns your bookmarks into a searchable AI knowledge base — entirely on-device, powered by Chrome's built-in Gemini Nano.