日本語

SORTING CINEMA / LEARN

How to use the visualizer and compare sorting algorithm complexity

Sorting rearranges data by a criterion such as size. Even for the same task, the movement differs between methods that swap neighbours, methods that split the array, and methods that use the digits of numbers. Sorting Cinema visualises 21 sorting algorithms for free, and you can learn with sound, Step, races and a quiz.

For programming beginners and anyone demonstrating in class. Text and implementation: Karou

Cyan, pink and yellow bars on a navy background moving from random order to ascending order
An AI-generated cover illustration evoking sorting and sound. The actual procedures are shown in the text below and in the app.

How to use the visualizer

  1. Open bubble sort with 12 elements. In the bar view, bar height is the value and the horizontal position is the array index.
  2. Press Step to advance one operation at a time and read the explanation of each compare, swap or placement. Play runs continuously and Stop interrupts.
  3. Switch presets such as “nearly sorted”, “reversed” and “many duplicates” to see how the input changes the movement. Choose Sound OFF if you do not need sound.
  4. In the Race tab, choose 2–4 algorithms and compare them on the same input. In the Quiz tab, guess the algorithm from its movement.

You can choose a musical scale and four views: bars, circle, color and scatter. Starting with the bar view, few elements and a low speed makes the movement easier to follow.

Each of the 21 algorithms has its own page with steps, complexity, a trace and an FAQ: all 21 sorting algorithms.

Complexity and stability of common sorts

Time complexity describes how the amount of work grows as the number of elements n grows. With O(n²), doubling n roughly quadruples the main work; O(n log n) grows more gently. Stability is the property of keeping the original order of elements with equal keys.

Properties of typical array implementations. Differences specific to this app are noted below. Extra space includes the recursion stack.
AlgorithmBestAverageWorstExtra spaceStability
Bubble (with early exit)O(n)O(n²)O(n²)O(1)Stable
SelectionO(n²)O(n²)O(n²)O(1)Usually unstable
InsertionO(n)O(n²)O(n²)O(1)Stable
MergeO(n log n)O(n log n)O(n log n)O(n)Can be implemented stably
QuickO(n log n)O(n log n)O(n²)O(log n) average, O(n) worstUsually unstable
HeapO(n log n)O(n log n)O(n log n)O(1)Usually unstable

This app's bubble sort does not stop early even after a pass with no swaps, so it makes O(n²) comparisons even on sorted input. Quick sort uses the last element as the pivot, so partitions can become unbalanced on sorted, reversed or many-duplicates input. The table does not predict how long an animation takes.

Counting sort is roughly O(n + k), where k is the range of values, and radix sort is roughly O(d(n + b)) with d digits and base b. Because they depend on the value range or the number of digits, they are not always faster than comparison sorts. This app handles positive integers, and radix sort processes base-10 digits starting from the least significant digit.

What is the difference between quick sort and merge sort?

Quick sort splits elements into a smaller side and a larger side around a pivot and sorts each part recursively. Merge sort splits the array in halves and merges the sorted parts. Both have an average time complexity of O(n log n), but quick sort can degrade to O(n²) when partitions are unbalanced, while merge sort keeps O(n log n) by using extra array space.

Watch quick sort to follow the pivot landing in its final position. Watch merge sort to see short sorted runs grow longer as they are merged. Details: quick sort, merge sort.

Three exercises to understand sorting through movement

1. Where do large values move?

Step through bubble sort. Watch large values move right in one pass, and try to explain why the range being compared gets shorter in the next pass.

2. What changes when the input is nearly sorted?

Compare “nearly sorted” and “reversed” in insertion sort. Focus on how far each element moves left. This app's operation count does not include every conditional comparison, so think about movement and complexity separately.

3. How can you sort without comparing?

Watch counting sort use the number of occurrences of each value, and radix sort reorder digit by digit. Asking “what if the value range is very wide?” shows how their conditions of use differ.

The 21 algorithms included and the scope of the implementation

The visualisations are teaching implementations. Tim sort is a simplified version that insertion-sorts 8 elements at a time and then merges; it does not reproduce features of library Timsort such as natural run detection. Sleep sort is a simulation that assigns delays based on the value-sorted order. Odd-even and bitonic sort are also animated sequentially in this app and do not measure the performance of parallel machines.

Try bogo, stooge and slow sort with small arrays. For Play and Step they are automatically reduced to 8, 20 and 16 elements respectively. Bogo sort has a cap on the number of attempts, so it is not guaranteed to be sorted when it stops.

Theory references: Princeton Algorithms: Elementary Sorts, Mergesort, Quicksort.

FAQ

Is it free? Do I need to sign up?

The visualizer, races and quiz are free and work in the browser without an account.

What if there is no sound?

Audio starts after you press Play or another control. Check that Sound ON is selected, that the browser tab is not muted, and the device volume. The visualizer still works with sound off.

Does the race ranking tell me which sort is fastest?

The ranking depends on the input, the animation delays and how things are drawn. The operation count is the number of compares, swaps, writes and so on used for the visualisation, and it is counted differently between implementations. Judging real performance requires a benchmark under equal conditions and an understanding of complexity.

Can I see whether equal values keep their order?

You can try the many-duplicates preset, but this app does not display each element's original ID. Equal numbers alone cannot reveal stability, so it has to be checked separately with data that carries both a key and the original order.

Does it work on smartphones?

It works in browsers that support touch. On narrow screens, reduce the number of elements. The comparison table can be scrolled horizontally.

Back to the visualizer →