-- online Search!
Look, it's a kirupa!!!

Sorting Algorithm Lab

by kirupa   |   filed under Data Structures and Algorithms

Reading about a sorting algorithm is one thing. Watching it shuffle a pile of numbers into place, one comparison at a time, is another! This lab lets us run the same numbers through every sorting algorithm we've covered, from the humble Bubble Sort to the hybrid Timsort, and see exactly how much work each one does.

Sort the Numbers

Select the sort you want, and then hit Start Sort below to run the simulation.

Best time
O(n)
Average time
O(n²)
Worst time
O(n²)
Extra space
O(1)
Stable
Yes

Bars are numbers. Matching numbers get letters (a, b, c) so we can see whether a sort keeps them in their original order.

Comparing Swapped or written Smallest so far Final position Pivot or run
Ready. Choose Start Sort to begin.
Comparisons
0
Swaps
0
Array writes
0
Extra memory writes
0
Extra memory slots
0 now / 0 peak

Customize Numbers

Or Type Your Own

Sorting Algorithm Lab

Sort Result

No sort yet. Start Sort will show the counts for this algorithm.

Work each algorithm does on the current numbers
AlgorithmComparisonsSwapsArray writesPeak extra memoryStepsResult

Running Time and Memory

Here's how each algorithm scales as the number of items, n, grows. For Counting Sort, k is the range of values. For Radix Sort, d is the number of digits and k is the 10 possible digits. Extra space doesn't count the input itself. A stable sort keeps equal values in their original order.

AlgorithmBest timeAverage timeWorst timeExtra spaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
QuicksortO(n log n)O(n log n)O(n²)O(log n) to O(n)No
TimsortO(n)O(n log n)O(n log n)O(n)Yes
Counting SortO(n + k)O(n + k)O(n + k)O(n + k)Yes
Radix SortO(d · (n + k))O(d · (n + k))O(d · (n + k))O(n + k)Yes
BogosortO(n)O(n · n!)UnboundedO(1)No

What to Look For

Start with Compare All Nine on the default numbers. The quadratic sorts (Bubble, Selection, and Insertion) make hundreds of comparisons, while Merge Sort, Quicksort, and Timsort need far fewer. Counting Sort and Radix Sort make zero comparisons. They tally values instead, and they pay for that with extra memory.

Now use Customize to load Already sorted numbers. Bubble Sort, Insertion Sort, and Timsort notice the order and finish in a single pass, which is their O(n) best case. Selection Sort and Merge Sort do the same amount of work no matter what. Try Reversed next and watch Timsort flip the whole descending run around with a few swaps.

Keep an eye on Extra memory slots. Bubble, Selection, and Insertion Sort sort in place and never go above zero. Merge Sort needs a buffer as large as the input. Quicksort only needs a small to-do stack of ranges, because this lab always sorts the smaller side first. That keeps the stack at about log2(n) entries even when the pivots are unlucky. Timsort's buffer only needs to hold the smaller-looking left run of each merge.

Load Few unique values to check stability. The letters on matching numbers stay in a, b, c order for the stable sorts. Selection Sort and Quicksort can jumble them.

Finally, give Bogosort a try with five or six numbers, where it usually wins after a few hundred shuffles. With more numbers, the expected number of shuffles explodes (that's the n! in its running time), so the lab politely gives up after 5,000 shuffles instead of freezing your browser.

A few notes on how this lab differs from the tutorials: Merge Sort merges bottom-up instead of recursing, Quicksort uses the tutorial's middle-value partitioning with an explicit stack instead of recursive calls, and Timsort uses a minimum run of 4 to 8 items (real implementations use 32 to 64) and skips galloping mode so every comparison is visible. The results and the running times are the same. For the full stories, follow the tutorial link for any algorithm, or see Meet the Default Sorting Algorithms for what our favorite languages actually use.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--