Tuesday, December 21, 2010

C Program that will sort the numbers prompt by the user using Selection Sort method.


#include<stdio.h>

int main()
{
        int a[5],i,j,iMin,temp,n;

        printf("Enter the five no with any order : ");

        for(i=0;i<5;i++)
        {
          scanf("%d",&a[i]);
        }

        for(i=0;i<5;i++)
        {
                iMin=i;
                for(j=i+1;j<5;j++)
                {
                   if(a[j]<a[iMin])
                     iMin=j;
                }
                if(iMin!=i){
                        temp=a[i];
                        a[i]=a[iMin];
                        a[iMin]=temp;
                }
        }

        printf("Sorted list in ascending array\n");
        for(i=0;i<5;i++)
        {
          printf("%d\n",a[i]);
        }
}

No comments:

Post a Comment