Structure preview

Sorting an array: Bubble Sort, Selection Sort and Insertion Sort

Three ways to sort the same numbers: comparisons, swaps and step-by-step examples, with a visual laboratory.

Section: Algorithms Updated:
Articles /algoritmi-ordinamento-bubble-selection-insertion
Sorting an array: Bubble Sort, Selection Sort and Insertion Sort

9 min

An array is a sequence of values. Sorting it means arranging those values, for example from smallest to largest. The final order can be identical even when the paths to it are very different. We will use the small array [5, 2, 4, 1] throughout, then try the three methods on 12 elements in the visual laboratory.

Illustration for the Algorithms section
Section: Algorithms

First, comparisons and swaps

A comparison answers a question such as “Is 5 greater than 2?”. A swap exchanges the positions of two elements. In the laboratory each comparison and swap is a visible step; animation time is not the computer’s actual running time.

Bubble Sort: the largest value rises to the end

Compare adjacent elements from left to right. If the left one is larger, swap them. At the end of a pass, the largest value still being sorted has reached the right-hand end. Later passes can ignore it.

  1. Start at [5, 2, 4, 1]. Swap 5 and 2 → [2, 5, 4, 1].
  2. Swap 5 and 4 → [2, 4, 5, 1]. Then swap 5 and 1 → [2, 4, 1, 5]. Now 5 is final.
  3. On the next pass 2 and 4 stay put; swap 4 and 1 → [2, 1, 4, 5].
  4. On the last pass swap 2 and 1 → [1, 2, 4, 5].

It is easy to follow, but a large array may require many comparisons and swaps. If a complete pass makes no swap, the algorithm can stop early.

Selection Sort: find the minimum

Think of the array as a sorted part on the left and an unsorted part on the right. Find the smallest value in the right-hand part and swap it into its first free position. The sorted part grows by one element each time.

  1. The minimum in [5, 2, 4, 1] is 1. Swap 5 and 1 → [1, 2, 4, 5].
  2. Among [2, 4, 5], the minimum is already 2: no swap is needed.
  3. Among [4, 5], the minimum is already 4. The result is [1, 2, 4, 5].

It still makes many comparisons even if the array is sorted, but it makes at most one swap per position.

Insertion Sort: insert into the sorted part

The first element alone is already sorted. Take the next element and move it left until it reaches the right place within the sorted prefix. This laboratory shows that movement as a sequence of adjacent swaps.

  1. Start with [5]. Insert 2 before 5 to get [2, 5, 4, 1].
  2. Insert 4 between 2 and 5 → [2, 4, 5, 1].
  3. Insert 1 before all the others → [1, 2, 4, 5].

It works particularly well when the values are already nearly sorted: each new element has only a short distance to travel.

What really changes?

All three methods sort correctly, but they perform different operations. Bubble Sort compares neighbours over repeated passes; Selection Sort repeatedly finds the minimum and limits swaps; Insertion Sort gradually builds a sorted prefix. In the worst case, their work grows roughly as the square of the array length. With 12 values, understanding why each move happens is more useful than measuring milliseconds.

Open the laboratory: watch the swaps, pause and try all three methods on the same 12-number array →