Selection sort in C

In selection sort, we search the entire list for the smallest element(or largest for descending order), and then simply swap that element with the element at the beginning of the list. Again, find the smallest element in the list but, excluding the first element this time. And then swap that with the element at second position in the array. We just keep repeating the same for the whole length of array. This sorting is not very feasible for large data sets.

Following program implements selection sort in C.

[code language=”csharp”]
#include

int main()
{

int a,i,j,min,loc,temp,n;

printf(“Enter the total no. of elements in the array: “);
scanf(“%d”,&n);

for(i=0;i<n;i++) {=”” printf(“enter=”” the=”” a[%d]=”” element=”” of=”” array:=”” “,i);=”” scanf(“%d”,&a[i]);=”” }=”” for(i=”0;i<n;i++)” min=”a[i];” loc=”i;” for(j=”i+1;j<n;j++)” if(a[j]<min)=”” temp=”a[loc];” a[loc]=”a[i];” a[i]=”min;” printf(“\nthe=”” sorted=”” array=”” is:”);=”” is:=”” %d”,i+1,a[i]);=”” return=”” 0;=”” [=”” code]=”” <=”” p=””></n;i++)>

Sharing is caring!