DSA Visualizer
SORT

Sorting Algorithm Visualizer

Three classic sorts, each with its own step-by-step visualizer: animated bars, highlighted pseudocode and a running tally of the work done.

Pick a sort to step through

Bubble vs selection vs insertion sort

All three do the same job, putting a list in order, and all three do it inside the list they were given. What differs is how they move values around, and that changes how much work each one does on the same input.

Time and memory cost of bubble, selection and insertion sort
AlgorithmBestAverageWorstExtra memoryStable
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

n is the number of values in the list. Read O(n) as “the steps grow in line with the list” and O(n²) as “double the list, quadruple the steps”. If that notation is new, the Big-O Playground explains it from zero →

How they differ, in one line each

  • Bubble sort swaps neighbours that are the wrong way round, over and over. Lots of swaps, but it notices when the list is already sorted and stops.
  • Selection sort searches for the smallest value left and swaps it into place. Very few swaps, but it always does the full amount of comparing.
  • Insertion sort slides each value left into a sorted part that grows from the front. No swaps at all, and almost no work when the list is nearly sorted.

Which sort should you learn first?

Start with bubble sort to get used to reading two nested loops. Move to selection sort to see a loop that remembers something (min) between steps. Finish with insertion sort, the one you are most likely to meet in real code.

A good experiment: open each sort, load the Nearly sorted preset and compare the counters. Your list travels with you when you switch between the three, so it is the same input and the same goal, with very different step counts.

Comparing the three sorts

Which is fastest: bubble sort, selection sort or insertion sort?

On small or nearly sorted lists, insertion sort usually wins, because it stops comparing as soon as a value is in place. Selection sort always does the same amount of comparing, and bubble sort does the most swapping. On large, shuffled lists all three slow down in the same way, which is why faster sorts such as merge sort exist.

Why learn these sorts if faster ones exist?

They are the easiest place to learn how to trace a loop, count steps and compare two algorithms that solve the same problem. They also show up in real code: the sort built into Python switches to insertion sort for small chunks of data because it is so quick on short lists.

What does it mean for a sort to be stable?

A stable sort keeps equal values in the order they started in. If two students both scored 80, a stable sort leaves them in their original order. Bubble sort and insertion sort are stable. Selection sort is not, because its long-distance swap can jump one equal value over another.

How many comparisons does each sort make?

For a list of n values, selection sort always makes n × (n − 1) ÷ 2 comparisons, even if the list is already sorted. Bubble sort makes up to the same number, but only n − 1 on a sorted list, because a pass with no swaps lets it stop early. Insertion sort also needs only n − 1 on a sorted list and the full amount on a reversed one.

Open in DSA Visualizer ↗