Sort Visualizer

Watch sorting algorithms work step by step with animated bars and sound.

Algorithm code

Loading...

User Guide

Getting Started
  1. Select a preset algorithm from the dropdown above the editor, or write your own sorting code.
  2. Click Start to begin visualization. Each bar represents an array element — taller bars are larger values.
  3. Use Pause / Resume to control playback, or Stop to cancel execution.
  4. Choose item-count and speed presets to control array size and playback speed.
  5. Enable Audio to hear tones mapped to values — higher pitches correspond to larger numbers.

Tip

To write your own: call length() to get the array size, iterate with loops, use compare(i, j) to compare elements, and swap(i, j) or write(i, value) to rearrange them.

API Reference

Write your algorithm using read/write/swap/compare/length/finish.

const sort = () => {
  const n = length();
  for (let i = 0; i < n - 1; i++) {
    if (compare(i, i + 1) > 0) swap(i, i + 1);
  }
  finish();
};

sort();
read(i)→ number

Returns the value at index i. Counted as a read operation.

const val = read(0);
write(i, value)→ void

Sets the value at index i. Counted as a write operation.

write(0, 42);
swap(i, j)→ void

Swaps elements at indices i and j. Counted as a swap and two writes.

swap(0, 1);
compare(i, j)→ number

Compares values at i and j. Returns negative if arr[i] < arr[j], zero if equal, positive if greater. This is a number, not a boolean.

if (compare(i, j) > 0) swap(i, j);
length()→ number

Returns the total number of elements in the array.

const n = length();
finish()→ void

Stops execution immediately when sorting is complete.

finish();
Color Legend
Default — inactive element
Read — element being read
Write — element being written
Swap — elements being swapped
Compare — elements being compared
Sweep — completed pass
Common Mistakes

Use provided functions

Write your algorithm with read/write/swap/compare/length/finish.

Infinite loop

If your algorithm doesn't make progress, the overload detector will automatically stop it. Check your loop conditions.

Index out of bounds

Accessing an index outside 0 to length−1 returns undefined. Always check indices against length().

compare() returns a number

compare(i, j) returns negative / 0 / positive, not true or false. Use compare(i, j) > 0 to check ordering.

Algorithm Complexity
AlgorithmBestAverageWorstSpaceStable
BubbleO(n)O(n²)O(n²)O(1)Yes
SelectionO(n²)O(n²)O(n²)O(1)No
InsertionO(n)O(n²)O(n²)O(1)Yes
MergeO(n log n)O(n log n)O(n log n)O(n)Yes
QuickO(n log n)O(n log n)O(n²)O(log n)No
HeapO(n log n)O(n log n)O(n log n)O(1)No

Statistics

Idle
Elapsed
0ms
Reads
0
Writes
0
Swaps
0
Comparisons
0

About the Sort Visualizer

This tool animates classic sorting algorithms — bubble, merge, quick sort and more — as moving bar charts, with tones mapped to values so you can hear the progress. You can also write and run your own algorithm.

Custom sort code runs inside a Web Worker sandbox to keep the main thread responsive, and a safety stop triggers when a suspected infinite loop is detected.