code for impementing sorting using Selection Sort algorithm
//code for selection sort with intermediate PASS result
#include<stdio.h>
int selectsmallest(int ar[],int i,int m)
{
int j,loc,min,k;
min=ar[i];
loc=i;
for(j=i+1;j<m;j++)
{
if(ar[j]<min)
{
min=ar[j];
loc=j;
}
}
printf("\nPass %d :",i);
for(k=0;k<m;k++)
printf("%d",ar[k]);
printf("\n\nsmallest element is %d at %d position",ar[loc],loc);
printf("\nswap this value with %d position",i);
return loc;
}
void selectionsort(int ar[],int m)
{
int loc,j,i,k,temp;
for(i=0;i<m;i++)
{
loc=selectsmallest(ar,i,m);
//printf("%d",ar[loc]);
temp=ar[loc];
ar[loc]=ar[i];
ar[i]=temp;
}
}
void main()
{
int i,m,ar[10];
printf("\nEnter the length of the array : ");
scanf("%d",&m);
printf("\nEnter the values : ");
for(i=0;i<m;i++)
scanf("%d",&ar[i]);
selectionsort(ar,m);
printf("\nafter sorting : ");
for(i=0;i<m;i++)
printf("%d",ar[i]);
}
#include<stdio.h>
int selectsmallest(int ar[],int i,int m)
{
int j,loc,min,k;
min=ar[i];
loc=i;
for(j=i+1;j<m;j++)
{
if(ar[j]<min)
{
min=ar[j];
loc=j;
}
}
printf("\nPass %d :",i);
for(k=0;k<m;k++)
printf("%d",ar[k]);
printf("\n\nsmallest element is %d at %d position",ar[loc],loc);
printf("\nswap this value with %d position",i);
return loc;
}
void selectionsort(int ar[],int m)
{
int loc,j,i,k,temp;
for(i=0;i<m;i++)
{
loc=selectsmallest(ar,i,m);
//printf("%d",ar[loc]);
temp=ar[loc];
ar[loc]=ar[i];
ar[i]=temp;
}
}
void main()
{
int i,m,ar[10];
printf("\nEnter the length of the array : ");
scanf("%d",&m);
printf("\nEnter the values : ");
for(i=0;i<m;i++)
scanf("%d",&ar[i]);
selectionsort(ar,m);
printf("\nafter sorting : ");
for(i=0;i<m;i++)
printf("%d",ar[i]);
}
Comments
Post a Comment