C program to sort an array using selection sort


Couch ModePrint

Program:

#include<stdio.h>
main()
{
           int i,j,a[10],n,small,index,temp;
           clrscr();
           printf("\nEnter the number of array\n");
           scanf("%d",&n);
           printf("\nEnter the array elemnts\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
    {
     small=a[i];
     index=i;
      for(j=i+1;j<n;j++)
         {
            if(a[j]<small)
            {
             small=a[j];
             index=j;
             }
          }
       temp=a[index];
        a[index]=a[i];
       a[i]=temp;
      }
printf("sorted list is\n");
          for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}


Output:

Enter the size of array
5

Enter the array elemnts
6
9
-6
-3
-2
sorted list is

-6      -3      -2      6       9


Thank Your Valuable Comment Tool to Insert codes in Comments : Convert My Code. Hide

Select to parse the code (not required if code is already parsed):

Subscribe By Email

Join Our Newsletter

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!

We Respect Your Privacy

Popular Posts