Selection sort is an algorithm in which the smallest element from an unsorted list is selected in each iteration and placed at the beginning of the unsorted list.
Selection sort then selects the next-smallest element every time and swaps it into the correct place. This is why it is called selection sort
Steps
min
.min
min
to the front of the unsorted list.
Example :
Code
void SelectionSort(int A[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int min = i;//stores index of element with minimum value
for (int j = i + 1; j < i; j++)
{
if (A[j] < A[min])
min = j;
}
temp = A[min];
A[min]= A[j];
A[j] = temp;
}
}
O(N^2)
Since there are 2 loops each running for O(n)
, the complexity is O(N)*O(N)
= O(N^2)
.