Mathwords logoMathwords

Selection Sort — Definition, Formula & Example

Selection sort is a sorting algorithm that scans the unsorted part of a list, finds the smallest item, and swaps it into the next position at the front. It repeats this until the whole list is in order.

Given an array A[0..n−1]A[0..n-1], for each i=0,1,…,n−2i = 0, 1, \dots, n-2, find an index mm with A[m]=min⁡A[i..n−1]A[m] = \min A[i..n-1] and swap A[i]A[i] with A[m]A[m]. After pass ii, the prefix A[0..i]A[0..i] is sorted and holds the i+1i+1 smallest elements. This is the loop invariant that proves the algorithm correct. Selection sort makes exactly n(n−1)2\frac{n(n-1)}{2} comparisons on every input, so its running time is Θ(n2)\Theta(n^2). It performs at most n−1n-1 swaps and sorts in place. The standard array version is not stable.

Key Formula

C(n)=(n−1)+(n−2)+⋯+1=n(n−1)2C(n) = (n-1) + (n-2) + \cdots + 1 = \frac{n(n-1)}{2}
Where:
  • C(n)C(n) = Number of comparisons selection sort makes on a list of length n
  • nn = Number of items in the list

Worked Example

Problem: Use selection sort to put the list [8, 3, 6, 1, 5] in increasing order. Count the comparisons it makes.
Step 1: Pass 1 scans all five items. The minimum is 1, so swap it with 8 in the first position. Pass 2 scans [3, 6, 8, 5]. The minimum is 3, which is already in place, so no swap is needed.
[8,3,6,1,5]→[1,3,6,8,5]→[1,3,6,8,5][8, 3, 6, 1, 5] \to [1, 3, 6, 8, 5] \to [1, 3, 6, 8, 5]
Step 2: Pass 3 scans [6, 8, 5]. The minimum is 5, so swap it with 6. Pass 4 scans [8, 6]. The minimum is 6, so swap it with 8. The last item is now in place automatically.
[1,3,6,8,5]→[1,3,5,8,6]→[1,3,5,6,8][1, 3, 6, 8, 5] \to [1, 3, 5, 8, 6] \to [1, 3, 5, 6, 8]
Step 3: Count the comparisons. Pass kk compares across the n−k+1n-k+1 unsorted items, which takes n−kn-k comparisons.
4+3+2+1=5⋅42=104 + 3 + 2 + 1 = \frac{5 \cdot 4}{2} = 10
Answer: The sorted list is [1, 3, 5, 6, 8]. It took 4 passes, 10 comparisons, and 3 actual swaps.

Why It Matters

In a first data structures or algorithms course, selection sort is often the first algorithm you analyze with a loop invariant and a Θ(n2)\Theta(n^2) count. It uses at most n−1n-1 swaps, so it can make sense when writing to memory is costly, as with flash storage. The same greedy rule of "take the best remaining choice" drives Prim's algorithm for minimum spanning trees.

Common Mistakes

Mistake: Calling selection sort "sinking sort" or treating it as the same algorithm as bubble sort.
Correction: "Sinking sort" is a standard alternate name for bubble sort, not selection sort. Bubble sort swaps adjacent out-of-order pairs many times per pass. Selection sort finds the minimum first and then makes at most one swap per pass.

Related Terms

  • Minimum Spanning Tree — Prim's algorithm repeatedly selects the minimum edge, like selection sort
  • Nearest Neighbor Algorithm — Another greedy method that picks the closest remaining option
  • Shellsort — Another in-place comparison sort, built on insertion sort with gaps
  • Algorithm — General idea of a step-by-step procedure