Selection Sort Visualization

How Selection Sort Works

Selection Sort divides the input list into two parts: a sorted portion at the left end and an unsorted portion at the right end. Initially, the sorted portion is empty and the unsorted portion is the entire list. The algorithm proceeds by finding the smallest element in the unsorted portion, swapping it with the leftmost unsorted element, and moving the subset boundaries one element to the right.

function selectionSort(array):
    for i from 0 to array.length - 1:
        minIndex = i
        for j from i + 1 to array.length - 1:
            if array[j] < array[minIndex]:
                minIndex = j
        swap array[i] and array[minIndex]
        visualize(array, i, minIndex)