PythonMastery
Python programs · Functions · beginner

Selection sort in Python

Sort a list by finding the smallest remaining item and swapping it into place, one position at a time. Includes a trace of each pass.

Selection sort finds the smallest item in the list and swaps it into the first position. Then it finds the smallest of the remaining items and swaps it into the second position, and so on until the end.

The program

python
def selection_sort(items):
    items = items.copy()               # leave the caller's list unchanged
    n = len(items)
    for i in range(n - 1):
        smallest = i
        for j in range(i + 1, n):
            if items[j] < items[smallest]:
                smallest = j
        items[i], items[smallest] = items[smallest], items[i]
    return items

numbers = [64, 25, 12, 22, 11]
print(selection_sort(numbers))
print(numbers)
print(selection_sort([]))
print(selection_sort([3, -1, 3, 0]))
output
[11, 12, 22, 25, 64]
[64, 25, 12, 22, 11]
[]
[-1, 0, 3, 3]

How it works

Watching the passes

Printing the list after each swap shows the sorted part growing from the left:

python
items = [64, 25, 12, 22, 11]
for i in range(len(items) - 1):
    smallest = i
    for j in range(i + 1, len(items)):
        if items[j] < items[smallest]:
            smallest = j
    items[i], items[smallest] = items[smallest], items[i]
    print(items)
output
[11, 25, 12, 22, 64]
[11, 12, 25, 22, 64]
[11, 12, 22, 25, 64]
[11, 12, 22, 25, 64]

Selection sort always makes the same number of comparisons, about n²/2 for n items, even when the list is already sorted. So, like bubble sort, it is slow on long lists. It makes at most n - 1 swaps, which is fewer than bubble sort. It is not stable: the long swaps can change the order of equal items. In real code, use sorted(items).

Learn the idea: Loops: for, while, and the Pythonic Patterns, Lists & Sequences