Search Your Query

Breaking

Sunday, July 14, 2019

Selection Sort

Selection Sort

Program Code:

#include<stdio.h>
#include<stdlib.h>
int selection(int *a,int n);
int main()
{
int *a,i,n;
printf("ENter the size of the array\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter the %d number element\n",i+1);
scanf("%d",&a[i]);
}
selection(a,n);
printf("The sorted array is-->\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
int selection(int *a,int n)
{
int i,j,loc,min,temp;
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
if(min!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
return (loc);

}

Hence, //comming soon

Input:

ENter the size of the array
5
Enter the 1 number element
74
Enter the 2 number element
89
Enter the 3 number element
41
Enter the 4 number element
12
Enter the 5 number element
63

Output:

The sorted array is-->
12
41
63
74
89

No comments:

Post a Comment