Skip to content

Sorting Algorithm Comparison

Run several sorting algorithms on the same data at the same time and watch who wins.

Algorithms in the race
Step 1 of 1

Each tick, every algorithm does one step: one comparison, swap or array write. The fewer steps an algorithm needs, the sooner it finishes.

Complexity of the sorting algorithms
AlgorithmBestAverageWorstExtra spaceStable
Bubble sort O(n) O(n²) O(n²) O(1) Yes
Selection sort O(n²) O(n²) O(n²) O(1) No
Insertion sort O(n) O(n²) O(n²) O(1) Yes
Merge sort O(n log n) O(n log n) O(n log n) O(n) Yes
Quick sort O(n log n) O(n log n) O(n²) O(log n) stack No
Heap sort O(n log n) O(n log n) O(n log n) O(1) No
Counting sort O(n + k) O(n + k) O(n + k) O(n + k) Yes
Radix sort O(d × (n + b)) O(d × (n + b)) O(d × (n + b)) O(n + b) Yes

Which sorting algorithm is fastest? It depends on the data. This page races up to eight algorithms side by side on exactly the same array: bubble sort, selection sort, insertion sort, merge sort, quick sort, heap sort, counting sort and radix sort. Every tick, each algorithm performs one step (a comparison, a swap or an array write), so the one that needs the fewest steps finishes first.

Change the input and the result changes. On random data the O(n log n) sorts leave the O(n²) ones far behind; on nearly sorted data insertion sort suddenly wins; on reversed data quick sort with a last-element pivot slows right down. A complexity table under the race sums up best, average and worst cases for each algorithm.

How to use the Sorting Algorithm Comparison tool

  1. Tick the algorithms you want in the race.
  2. Pick the array size and the input: random, nearly sorted, reversed or few unique values.
  3. Press Play. Each lane shows its bars, comparisons and swaps, and its rank when it finishes.
  4. Use Step, Back and the position slider to compare the lanes at any moment, then press New race for fresh data.

Why use ToolOrca Sorting Algorithm Comparison

  • Up to eight sorting algorithms racing on the same data
  • Four input types and sizes from 10 to 100
  • Comparisons, swaps and finishing rank for each lane
  • Play, pause, step, step back and speed controls
  • Complexity table for all algorithms
  • Works on phones and tablets

Comparing sorting algorithms by Big O

The simple sorts (bubble, selection, insertion) take O(n²) time on average: doubling the array quadruples the work. Merge sort, heap sort and quick sort (on average) take O(n log n): doubling the array only slightly more than doubles the work. For 100 values that is roughly 5,000 steps against 700; for a million values it is 500 billion against 20 million.

No comparison-based sort can beat O(n log n) in general. Counting sort and radix sort get around that limit by not comparing at all, using the values as indexes, which works for whole numbers in a limited range.

Which sorting algorithm should I use?

In real code: use your language’s built-in sort. Python and Java (for objects) use Timsort, a stable merge and insertion sort hybrid; C++ std::sort uses introsort, a quick sort with heap sort and insertion sort fallbacks. Small or nearly sorted arrays: insertion sort. Need stability or guaranteed O(n log n): merge sort. Need O(1) extra memory and a guarantee: heap sort. Fastest in-memory average: quick sort with a good pivot. Small integer keys: counting sort; many fixed-width integers: radix sort.

Why the race uses steps, not seconds

Measuring milliseconds in a browser depends on your device and on what else is running. Counting steps (comparisons, swaps and writes) shows the algorithmic difference directly. Real running times also depend on cache behaviour and constant factors, which is why quick sort often beats merge sort in practice despite similar step counts.

Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.

Frequently asked questions

What is the fastest sorting algorithm?

There is no single winner. Quick sort is usually fastest on random data in memory, insertion sort wins on small or nearly sorted data, and counting or radix sort win on small integer keys. Built-in hybrid sorts combine these strengths.

Why does insertion sort win on nearly sorted input?

Its running time grows with the number of out-of-order pairs. A nearly sorted array has very few, so insertion sort does close to n steps.

Why is quick sort slow on sorted or reversed input here?

This version uses the last element as the pivot. On sorted or reversed data that pivot is always the largest or smallest value, so each partition only removes one element and the time becomes O(n²).

Which sorting algorithms are stable?

Bubble, insertion, merge, counting and radix sort are stable. Selection, quick and heap sort are not, in their usual forms.

What sorting algorithm does Python or Java use?

Python’s sorted() and list.sort() use Timsort. Java uses Timsort for objects and a dual-pivot quick sort for primitive arrays. C++ std::sort uses introsort.