beginnersorting

Selection Sort

Selection Sort

Selection sort divides the array into two parts: a sorted portion at the beginning and an unsorted portion at the end. It repeatedly selects the smallest element from the unsorted portion and swaps it with the first unsorted element. While simple to understand, it always performs O(n²) comparisons regardless of input order.

How It Works

  1. Find the minimum element in the unsorted portion of the array
  2. Swap it with the first element of the unsorted portion
  3. Advance the boundary between sorted and unsorted by one position
  4. Repeat until the entire array is sorted

Step-by-Step Example

Given the array [64, 25, 12, 22, 11]:

  • Pass 1: Find minimum (11) in full array. Swap with index 0 → [11, 25, 12, 22, 64]
  • Pass 2: Find minimum (12) in [25, 12, 22, 64]. Swap with index 1 → [11, 12, 25, 22, 64]
  • Pass 3: Find minimum (22) in [25, 22, 64]. Swap with index 2 → [11, 12, 22, 25, 64]
  • Pass 4: Find minimum (25) in [25, 64]. Already at index 3 → [11, 12, 22, 25, 64]

Code Snippet

def selection_sort(numbers: list[int]) -> list[int]:
    """Sort by repeatedly finding the minimum from the unsorted portion."""
    array_length = len(numbers)

    for current_position in range(array_length - 1):
        min_index = current_position

        # Find the minimum element in the remaining unsorted array
        for search_index in range(current_position + 1, array_length):
            if numbers[search_index] < numbers[min_index]:
                min_index = search_index

        # Swap the found minimum with the first unsorted element
        if min_index != current_position:
            numbers[current_position], numbers[min_index] = (
                numbers[min_index],
                numbers[current_position],
            )

    return numbers

Complexity Analysis

Time Complexity: O(n²)

  • Best case — O(n²): Even if the array is already sorted, selection sort still scans the entire unsorted portion to confirm the minimum. It always makes (n-1) + (n-2) + ... + 1 = n(n-1)/2 comparisons.
  • Worst case — O(n²): Same number of comparisons regardless of input order.
  • Average case — O(n²): No input arrangement changes the comparison count.

The key insight is that selection sort is not adaptive — it cannot exploit existing order in the data.

Space Complexity: O(1)

Selection sort is in-place, requiring only a constant amount of extra memory for the index variables and the swap operation.

Comparison with Other O(n²) Sorts

PropertySelection SortInsertion SortBubble Sort
ComparisonsAlways n²/2Best: n, Worst: n²/2Best: n, Worst: n²/2
SwapsAt most n-1Up to n²/2 shiftsUp to n²/2
StableNoYesYes
AdaptiveNoYesYes

Selection sort's advantage is minimal writes — it performs at most n-1 swaps. This matters when writing to memory is expensive (e.g., flash memory or EEPROM).

When to Use Selection Sort

  • Minimizing writes: When memory writes are costly (flash storage), selection sort's O(n) swaps beat insertion sort's O(n²) shifts.
  • Teaching purposes: Its clear "find minimum, place it" logic makes it easy to visualize and understand.
  • Small arrays: For very small inputs (n < 20), the overhead difference between algorithms is negligible.

When to Avoid It

  • Large datasets: O(n²) with no adaptivity makes it impractical for n > 100.
  • Nearly sorted data: Unlike insertion sort, it cannot take advantage of existing order.
  • Stability required: Selection sort is not stable — equal elements may change their relative order during swaps.

Key Properties

PropertyValue
In-placeYes
StableNo
AdaptiveNo
Minimum swapsYes (at most n-1)