Home » , » Selection Sort in C

Selection Sort in C

Selection Sort in C

Let us see how to implement Selection Sort in C Language.


What is Selection Sort?

The selection sort is a sorting method that yields a sorted array. It does so by repeatedly finding the smallest element in the array and interchanging it with the element at the starting of the unsorted part.


Given example explains the steps required in Selection Sort.


The array before sorting starts is: 



Now, the smallest element in this array is 1 which is at position 3. This element is interchanged with 3 at position 0. So the array obtained is:


After the above step, first element of the array is sorted. Out of the remaining 4 elements, the smallest element is 2, at position 4. This is exchanged with 9 which is at position 1. The array obtained is:


The first 2 elements of the array are sorted. Out of the remaining elements, smallest is 3 at position 3. This is replaced with 5 at position 2. The array obtained is:


The first 2 elements of the array are sorted. Out of the remaining elements, smallest is 3 at position 3. This is replaced with 5 at position 2. The array obtained is:


The first 2 elements of the array are sorted. Out of the remaining elements, smallest is 3 at position 3. This is replaced with 5 at position 2. The array obtained is:


Finally the sorted array is obtained. This is given as:


Source Code

The source code for Selection Sort is as follows:


Output

The output obtained for the above code is:







0 comments:

Post a Comment

Popular Posts

GENRES

 
Copyright ©

latest code update bloggingskillBest coding website in india

Created By Sora Templates | Distributed By Gooyaabi Templates