Selection sort
Overview
Selection sort is a simple sorting algorithm that finds the smallest element in the unsorted part of the list and swaps it with the first unsorted element.
Visualization
Pseudocode
1:
procedure SelectionSort(A) 2:
3:
while i<∣A∣ do 4:
5:
while j<∣A∣ do 6:
if A[j]>A[k] then 7:
8:
9:
10:
Swap(A[i],A[k]) Time Complexity
Selection sort has the same time complexity regardless of the input.
It takes (n−1)+(n−2)+…+1=2(n−1)+1 comparisons to find the smallest element and n−1 swaps,
resulting in a time complexity of 2(n−1)+1(n−1)=O(n2).