Simple Sorting (Bubble, Selection, Insertion)

Three easy O(n²) sorts, how many swaps and comparisons they make, and when they are still useful.

Sorting arranges items in order. It is one of the most studied problems in computing, because sorted data makes searching (binary search), finding duplicates and merging much faster. This lesson covers three simple algorithms; the next one covers the fast ones.

Two properties matter when comparing sorts:

  • Stable: equal keys keep their original relative order.
  • In place: only O(1) extra memory is used.

Bubble sort

Repeatedly walk through the array and swap neighbours that are out of order. After pass k, the k largest items have "bubbled" to the end. If a pass makes no swaps, the array is sorted and we can stop early.

Selection sort

For each position i, find the smallest item in the unsorted part and swap it into position i. It makes at most n − 1 swaps, the fewest of the three.

Insertion sort

Take the items one by one and insert each into its correct place in the already-sorted part on the left, shifting bigger items right, like sorting playing cards in your hand. It is very fast on nearly sorted data.

key = a[i], j = i - 1j >= 0 and a[j] > key?Yesa[j + 1] = a[j] (shift right)j = j - 1Noa[j + 1] = keynext i
Insertion sort: placing one item, key = a[i]
import java.util.Arrays;

public class Main {
    static int comparisons, swaps;

    static void bubble(int[] a) {
        for (int pass = 0; pass < a.length - 1; pass++) {
            boolean swapped = false;
            for (int j = 0; j < a.length - 1 - pass; j++) {
                comparisons++;
                if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; swaps++; swapped = true; }
            }
            if (!swapped) break;              // already sorted: stop early
        }
    }

    static void selection(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++) {
                comparisons++;
                if (a[j] < a[min]) min = j;
            }
            if (min != i) { int t = a[i]; a[i] = a[min]; a[min] = t; swaps++; }
        }
    }

    static void insertion(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int key = a[i], j = i - 1;
            while (j >= 0) {
                comparisons++;
                if (a[j] <= key) break;
                a[j + 1] = a[j];              // shift right (counted as a move)
                swaps++;
                j--;
            }
            a[j + 1] = key;
        }
    }

    static void run(String name, int[] data, java.util.function.Consumer<int[]> sorter) {
        int[] a = data.clone();
        comparisons = swaps = 0;
        sorter.accept(a);
        System.out.printf("%-10s %s  comparisons=%d swaps/shifts=%d%n", name, Arrays.toString(a), comparisons, swaps);
    }

    public static void main(String[] args) {
        int[] data = {5, 1, 4, 2, 8};
        run("bubble", data, Main::bubble);
        run("selection", data, Main::selection);
        run("insertion", data, Main::insertion);
    }
}
Output
bubble     [1, 2, 4, 5, 8]  comparisons=9 swaps/shifts=4
selection  [1, 2, 4, 5, 8]  comparisons=10 swaps/shifts=2
insertion  [1, 2, 4, 5, 8]  comparisons=7 swaps/shifts=4

Inversions

An inversion is a pair of positions i < j with a[i] > a[j]. Each swap of neighbours in bubble sort, and each shift in insertion sort, fixes exactly one inversion. So both make exactly as many swaps/shifts as there are inversions: [5, 1, 4, 2, 8] has 4 inversions, (5,1), (5,4), (5,2) and (4,2), which matches the output above.

AlgorithmBestAverageWorstStableIn place
Bubble sort (with early exit)O(n)O(n²)O(n²)yesyes
Selection sortO(n²)O(n²)O(n²)noyes
Insertion sortO(n)O(n²)O(n²)yesyes

GATE-style practice questions

GATE-styleNAT · numerical answer

How many inversions does the array [4, 3, 2, 1, 5] contain?

Show answer and explanationAnswer: 6

Every pair among 4, 3, 2, 1 is inverted: C(4, 2) = 6 pairs. 5 is larger than everything before it, so it adds none.

GATE-styleNAT · numerical answer

Insertion sort is run on an array that is already sorted in increasing order. How many key comparisons does it make for n = 10?

Show answer and explanationAnswer: 9

Each new item is compared once with its left neighbour, finds it smaller or equal, and stops. Items 2 to 10 make one comparison each, so n − 1 = 9 comparisons, which is why insertion sort is O(n) in the best case.

GATE-styleMSQ · one or more correct answers

Which of these sorting algorithms are stable in their standard form? (Select all that apply.)

Show answer and explanationAnswer: A, B, D

Bubble and insertion sort only move an item past a strictly greater neighbour, and merge sort takes from the left half on ties, so equal keys keep their order. Selection sort's long-distance swap can jump an item over an equal one, for example [2a, 2b, 1] becomes [1, 2b, 2a].

GATE-styleMCQ · one correct answer

Selection sort is run on an array of n distinct elements. What is the maximum number of swaps it performs?

Show answer and explanationAnswer: C

Selection sort does at most one swap per outer iteration, and there are n − 1 outer iterations. That makes it useful when writing to memory is expensive.