Selection Sort Using C

Description

 This program is used to sort elements using Selection Sort.


#include<stdio.h>
void sort();
main()
{
 int choice;
 do
 {
  printf("\n----MENU----\n1.Enter elements\n2.EXIT\n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:sort();break;
   case 2:printf("\n----Thank you----\n");break;
   default:printf("\nINVALID INPUT !!!!");
  }
 }while(choice!=2);
}


void sort()
{
 int i,j,temp,n,s,flag;
 int a[100];
 printf("Enter no. of elements to be sorted\n");
 scanf("%d",&n);
 printf("\nEnter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 
 for(i=0;i<n;i++)
 {
  s=i;
  for(j=i;j<(n-1);j++)
  {
   if(a[i]>a[j+1])
   {
    temp=a[i];
    a[i]=a[j+1];
    a[j+1]=temp;
   }
  }
 }
 printf("\n\nSorted Elements are :\n");
 for(i=0;i<n;i++)
 {
  printf("%d\n",a[i]);
 }
}


OUTPUT

Selection Sort Output 1 Selection Sort Output 2

Labels: