Sort elements using selection sort - C Program

C program to sort 'n' numbers using selection sort.

Solution:

In selection sort, find the smallest element in array and swap it with the first position element in array. Then find the second largest element in array and exchange it with second position element in array. This process repeats until the end of an array. At the end of this process all the elements are sorted in ascending order.

#include<stdio.h>
int main()
{
      int i,j,size,loc,temp,min,a[20];
      printf("\tSelection sort\n");
      printf("-----------------------------------\n");
      printf(" How many numbers you want to sort?: ");
      scanf("%d",&size);
      printf("\n Enter %d numbers: \n",size);

      for(i=0;i<size;i++)
      {
            scanf("%d",&a[i]);
      }
      for(i=0;i<size-1;i++)
      {
            min=a[i];
            loc=i;
            for(j=i+1;j<size;j++)
            {
                  if(min>a[j])
                  {
                        min=a[j];
                        loc=j;
                  }
            }
            temp=a[i];
            a[i]=a[loc];
            a[loc]=temp;
      }
      printf("\n Sorted numbers after using selection sort: \n");
      for(i=0;i<size;i++)
      {
            printf("  %d ",a[i]);
      }
      return 0;
}


Output:

selection sort