Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Visualizer › Sorting

Selection Sort

Finds the smallest element of the unsorted part and swaps it to the front, growing the sorted part by one element each pass.

How it works

Complexity

Code (Python)

def selection_sort(a):
    n = len(a)
    for i in range(n - 1):
        m = i
        for j in range(i + 1, n):
            if a[j] < a[m]:
                m = j
        a[i], a[m] = a[m], a[i]
    return a

Practise: Arrays problems