Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Sort - Selection Sort

/********************************************/
/*PROGRAM TO PERFORM SELECTION SORT*/
/********************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
       int a[10],n,i,j,temp,min,loc;

       clrscr();

       printf("Enter the size of array: ");
       scanf("%d",&n);
       printf("\nEnter the elements of array:\n");
       for(i=0;i<n;i++)
       {
              scanf("%d",&a[i]);
       }
       for(i=0;i<n;i++)
       {   
              min=a[i];
              loc=i;
              for(j=i+1;j<n;j++)
              {
                     if(min>a[j])
                     {
                            min=a[j];
                            loc=j;
                     }
              }
              temp=a[i];
              a[i]=a[loc];
              a[loc]=temp;
         }

          printf("\nAfter sorting the elements of array are:\n");
          for(i=0;i<n;i++)
          {
                    printf("%d ",a[i]);
          }
          getch();
}

Share this article   |    Print    |    Article read by 859 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question