Pages

Ads 468x60px

Saturday, 2 June 2012

C Program For SELECTION SORTING


C Program For SELECTION SORTING



Searching and sorting have been popular operations in elementry data structures. Although now a days many advanced sorting techniques are used to sort a set of data, the basic sorting methods are still popular and are the used frequently. Here C code for selection sort is given. The algorithm for selection sort is quite simple. In the first iteration, 1st element is compared against all the other elements (from array index 1 to array index n). In the second iteration 2nd element is compared with the elements from array index 2 to n. This process is repeated n-1 times.



CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[20],j,z;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
i=1;
while(i<=n)
{
for(j=i+1;j<=n;j++)
{
z=a[i];
if(a[i]>a[j])
{
a[i]=a[j];
a[j]=z;
}
}
i++;
}
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

0 comments:

Post a Comment

Total Pageviews