Skip to content
Code it · Write it

Write selection sort

Write selection_sort(arr) (Python) or selectionSort(arr) (JavaScript/TypeScript). It sorts arr in place, smallest first.

For each position i, scan the rest of the array for the smallest element, then swap it into position i. That makes exactly one swap per position, however mixed up the array is.

def selection_sort(arr):
    # Sort arr in place, smallest first.
    # Find the smallest remaining element and swap it into place.
    pass