Selection Sort
#include<stdio.h>
#include<conio.h>
//To define function
void selectionsort(int [], int);
void main()
{
int a[100],i,n;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&a[i]);
}
selectionsort(a,n);
getch();
}
//To sort the elements
void selectionsort(int a[], int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nSorted array is: ");
for(j=0;j<n;j++)
{
printf("%d ",a[j]);
}
}
0 Comments