/*Selection Sort*/
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,position,swap;
float a[100];
printf("enter no. of elements:");
scanf("%d",&n);
printf("enter %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)
{
if(a[position]>a[j])
position=j;
}
if(position!=i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("sorted list in ascending order is:\n");
for(i=0;i<n;i++)
printf("%f\n",a[i]);
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,position,swap;
float a[100];
printf("enter no. of elements:");
scanf("%d",&n);
printf("enter %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)
{
if(a[position]>a[j])
position=j;
}
if(position!=i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("sorted list in ascending order is:\n");
for(i=0;i<n;i++)
printf("%f\n",a[i]);
return(0);
}
Comments
Post a Comment