Thursday, May 27, 2010

Selection Sort Method

// Program to Sort Data in ascending order using Selection Sort Method


#include< stdio.h >
#include< conio.h >
void main()
{
//variable declaration
int a[10],i,j,n,temp,small,position;
clrscr();
printf("Enter Total Number of Elements :: ");


scanf("%d",&n);


//Input array element
printf("\n Enter Elements :: ");
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}


//Print Unsorted List
printf("\n Unsorted List :: \n");
for(i=0;i< n;i=i+1)
{
printf("\t%d",a[i]);
}


//Sort data in ascending order
for(i=0;i< =n;i++)
{ small=a[i];
 position=i;
for(j=i;j< =n;j=j+1)
{
 // find smallest element in the range i to n
if (a[j]< small)
{
small=a[j];
position=j;
}
}
// interchange elements
temp=a[i];
a[i]=a[position];
a[position]=temp;
}


//Print Sorted List
printf("\n Sorted List :: \n");
for(i=0;i< n;i=i+1)
{
printf("\t%d",a[i]);
}
getch();
}

No comments:

Post a Comment