1. Selection sort
Code for Selection Sort
#include<stdio.h>
void selection_sort(int a[], int n); //function declare
int main()
{
int n,i,a[100];
printf("Enter
how many noumbers ??\n");
scanf("%d",&n);
printf("Put
the numbers are in unsorted oriented\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The
Inputed array::\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
selection_sort(a,n); //function call
printf("\n\nThe Sorted array is::\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
return 0;
}
void selection_sort(int a[], int n) //function definition
{
int
temp,i,j,min,loc;
min=a[0];
for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
if(loc!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
}
}output ::
No comments:
Post a Comment