Here we find the smallest element in the list and swap it with the first element. Then the second smallest, swapping with second element and so on. This is done till (n-1) times. And number of swaps required is n-1.

5 4 3 2 1

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

Well, the time complexity is O(n*n) which is same as https://sowmyaravidas.wordpress.com/2012/10/06/bubble-sort/

But selection is better since number of swaps required is less.

void selection_sort(int unsorted[],int number_of_elements) { int minimum; int temp; int minimum_position; for(int i=0;i<number_of_elements;i++) { minimum=unsorted[i]; for(int j=i+1;j<number_of_elements;j++) { if(minimum>unsorted[j]) { minimum=unsorted[j]; minimum_position=j; temp=unsorted[i]; unsorted[i]=unsorted[minimum_position] ; unsorted[minimum_position]=temp; } } } }

Complete implementation of the code can be found at https://github.com/sowmyaravidas/Sorting-Algorithms

## Recent Comments