Sort Ready
size 8
stable yes
in place yes
  • Comparing
  • Swap / write
  • Pivot / key
  • Sorted

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Two words to know: stable and in place

A sort is STABLE if equal values keep their original order (important when sorting records by one field, e.g. students by marks). A sort is IN PLACE if it needs only a few extra variables, not a second array. Merge, radix and bucket sort need extra space; heap and quick sort do not.

// Stable: two students with 90 marks keep their order
//   Asha 90, Ravi 75, Meera 90
//   → Ravi 75, Asha 90, Meera 90     (stable)
//   → Ravi 75, Meera 90, Asha 90     (not stable)

void swap(int *x, int *y) {
    int t = *x;  *x = *y;  *y = t;
}

Bubble · Selection · Insertion

  1. Bubble: swap wrong-order neighbours; the largest bubbles to the end.
  2. Selection: find the smallest, swap it to the front.
  3. Insertion: slide each value left into the sorted part.
  4. All O(n²) — fine for small arrays.

Merge sort

  1. Split the array in half, again and again.
  2. A single value is already sorted.
  3. Merge two sorted halves by taking the smaller front value.
  4. Always O(n log n), needs a temp array.

Quick sort

  1. Pick a pivot (here: the last value).
  2. Partition: smaller values left, the rest right.
  3. The pivot is now in its final place.
  4. Quick sort both sides. Usually the fastest in practice.

Heap sort

  1. Children of index i are 2i + 1 and 2i + 2.
  2. Build a max-heap: every parent ≥ its children.
  3. Swap the root (maximum) to the end; shrink the heap.
  4. Sift the new root down. O(n log n), in place.

Radix sort

  1. Put values in 10 buckets by their ones digit.
  2. Collect buckets 0 → 9, keeping order inside each.
  3. Repeat for tens, hundreds, …
  4. No comparisons: O(d · (n + 10)).

Bucket sort

  1. Split the value range into k buckets.
  2. Drop each value into its bucket.
  3. Sort each (small) bucket.
  4. Join the buckets. O(n + k) for evenly spread values.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 BUBBLE-SORT(A, n)
2 for i ← 0 to n − 2
3 swapped ← false
4 for j ← 0 to n − 2 − i
5 if A[j] > A[j + 1] then swap A[j], A[j + 1]; swapped ← true
6 if not swapped then stop
Complexity

How fast is each operation?

n = number of values, d = number of digits, k = number of buckets. The O(n log n) comparison sorts cannot be beaten by any sort that only compares values; radix and bucket sort avoid comparisons.

Operation Best Average Worst Speed
Bubble sort O(n) O(n²) O(n²) Grows with n
Selection sort O(n²) O(n²) O(n²) Grows with n
Insertion sort O(n) O(n²) O(n²) Grows with n
Merge sort O(n log n) O(n log n) O(n log n) Grows with n
Quick sort O(n log n) O(n log n) O(n²) Grows with n
Heap sort O(n log n) O(n log n) O(n log n) Grows with n
Radix sort O(d·(n + 10)) O(d·(n + 10)) O(d·(n + 10)) Grows with n
Bucket sort O(n + k) O(n + k) O(n²) Grows with n

All eight at a glance

Algorithm Stable? In place? Extra space Good for
Bubble Yes Yes O(1) Teaching; detecting an already sorted array
Selection No Yes O(1) When swaps (writes) are expensive
Insertion Yes Yes O(1) Small or nearly sorted arrays
Merge Yes No O(n) Guaranteed O(n log n), linked lists, huge files
Quick No Yes O(log n) Fastest in practice on average
Heap No Yes O(1) O(n log n) worst case with no extra memory
Radix Yes No O(n + 10) Many integers with few digits
Bucket Yes No O(n + k) Values spread evenly over a range

Best and worst inputs

Algorithm Already sorted Reversed
Bubble (with early stop) O(n) — one pass, no swaps O(n²) — every pair swaps
Insertion O(n) — nothing shifts O(n²) — everything shifts
Selection O(n²) — still scans everything O(n²)
Quick (last element as pivot) O(n²) — the pivot is always the max O(n²) — the pivot is always the min
Merge / Heap O(n log n) O(n log n)
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

Which sort runs in O(n) on an array that is already sorted?

Question 2

After the first pass of bubble sort, which value is guaranteed to be in its final place?

Question 3

What does quick sort guarantee after one partition?

Question 4

Which of these sorts is NOT stable?

Question 5

Radix sort on [170, 45, 75, 90]: what order do you get after the ones-digit pass?

Question 6

Why does merge sort need O(n) extra memory?

Assessment

Sorting quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.