Chapter 3 · Simulation

Sorting Race

← Chapter 3

Sorting Race

Run several sorting algorithms on the same array and compare their progress, number of comparisons and number of swaps/moves.

How to use

  • Choose an array size (8–40) and press New Array to generate random values between 10 and 99.
  • Press Start Race. Each lane takes one basic step per tick, so the finishing order reflects how many steps each algorithm needs on this input.
  • Speed changes the tick length; pressing Start Race again restarts the race on the same array.

Algorithms

AlgorithmBestAverageWorstNotes
Bubble sortO(n²)*O(n²)O(n²)Adjacent swaps; stable
Insertion sortO(n)O(n²)O(n²)Shifts items; stable, fast on nearly sorted data
Selection sortO(n²)O(n²)O(n²)At most n − 1 swaps; not stable
Quick sortO(n log n)O(n log n)O(n²)Lomuto partition, last element as pivot

* This bubble sort has no early-exit flag, so it always makes n(n − 1)/2 comparisons.

In insertion sort, “moves” counts item shifts; in the other algorithms it counts swaps.