2014-05-18

C program for Selection Sort. Selection Sort is a simple-unstable-in-place Sorting Algorithm. Selection sort is efficient when memory is limited and over a small set of data, while make it more complex and inefficient on large set of data and become more worse as compare to insertion sort. You can also see other sorting algorithm implementation like, Heap sort, bubble sort etc.

Selection Sort Algorithm:

Algorithm scans through the data, looking for either the smallest or the largest element(depending upon you) in the set and then swaps it with the first or the last element of Data set. Now find the smallest item from the data set excluding the first that is already sorted, and replace that element with the Second element. Similarly the loop continues until the last two items in the data set. Now the Data is sorted.

Note: The above Selection sort, described uses swapping of data which makes the algorithm Unstable, while Selection sort can also be implemented as Stable sort, by selecting the smallest element and putting the element before the first element of the list i.e. instead of swapping the first element with the smallest element, we are sweeping the first element to second position, second to third and so on.

C program for Selection Sort – Unstable In-place:

Output of C program for Selection Sort



Output of C program for selection sort

Example of Selection Sort:

Still getting problem or confused how Selection Sort algorithm works, then see the images below, demonstrating the operation performed to sort the array:



Selecting the first smallest and swapping



Selecting Second smallest from the list and comparing with the second position

Selecting the third element and comparing with the remaining element and swapping

Already sorted

Sorted : C program for Selection Sort

Hope this example is helpful. Do comment below with your experience, problem or code. Your comment is very valuable to us.

The post C program for Selection Sort appeared first on cprogramto.com.

Show more