SELECTION SORT IN C PROGRAMMING:
A Selection sort is a type of sorting algorithm which finds the smallest element in the given array first and swaps with first element and then sorts the second element and then third it will continue until the entire array is sorted.PROGRAM FOR BUBBLE SORT:
#include<stdio.h>
int main()
{
int i,j,n,temp,min,a[100];
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("\n Sorted list is as follows\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
OUTPUT:
Enter the number of elements:6
Enter elements:56 43 29 87 22 10
Sorted list is as follows:10 22 29 43 56 87
0 Comments