Sunday, April 5, 2009

Write a C Program for Selection Sort

/* Selection Sort */

#include " stdio.h "
#include " conio.h "
main()
{
int a[10],n,i,j,k=0;
printf("enter array size");
scanf("%d",&n);
for(i=0;i < n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
getch();
}
selectionsort(int a[],int n)
{
int i,j,temp;
for(i=0;i < n;i++)
for(j=i+1;j < n;j++)
{
if( a[i] > a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
k++;
}
printf("after %d cycle elements in the list are ",k
printf("sorted order is");
for(i=0;i < n;i++)
printf("%4d",a[i]);
}
Related Posts with Thumbnails