Selection Sort Intro: Selection Sort is an in situ comparison based algorithm in which the list is split into 2 components, the sorted half at the left end and therefore the unsorted half at the right end. Initially, the sorted part is empty and the unsorted half is the entire list. The smallest element is chosen from the unsorted array and swapped with the left element, and that element becomes a vicinity of the sorted array. This method continues moving unsorted array boundary by one element to the right.

 

Explanations:

Find the smallest card. Swap it with the primary card.
Find the second-smallest card. Swap it with the second card.
Find the third-smallest card. Swap it with the third card.
Repeat finding the next-smallest card, and swapping it into the correct position till the array is sorted.

Selection Sort Implementation:

void selectionSort(int a[], int size)
{
  int i, j, min, temp;
  for(i=0; i < size-1; i++ )
  {
    min = i;   //setting min as i
    for(j=i+1; j < size; j++)
    {
      if(a[j] < a[min])   //if element at j is less than element at min position
      {
       min = j;    //then set min as j
      }
    }
   temp = a[i];
   a[i] = a[min];
   a[min] = temp;
  }
}

Detailed:

   SELECTION-SORT(A)
     for j ← 1 to n-1
          smallest ← j
           for i ← j + 1 to n
                   if A[ i ] < A[ smallest ]
                          smallest ← i
            Exchange A[ j ] ↔ A[ smallest ]