Xpode.com        Click here to Print this article.

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();
}



http://


Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=53

Click here to go on website