Here is the code for Insertion Sort, Bubble Sort and Selection Sort
Insertion Sort
#include <stdio.h>
main()
{
int i,j,key;
int a[5]={5,2,3,4,1};
for(i=1;i<5;i++)
{
key=a[i];
while(i>0 && a[i-1]>key) {
j=a[i];
a[i]=a[i-1];
a[i-1]=j;
--i;
}
}
int k;
for(k=0;k<5;k++)
{printf("%d ", a[k]);}
printf("\n");
}
Bubble Sort
#include <stdio.h>
main()
{
int i,j,x;
int a[5]={5,2,3,4,1};
for(i=0;i<5;i++) {
for(j=i+1;j<5;j++) {
if(a[i]>a[j]) {
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
int k;
for(k=0;k<5;k++) {printf("%d ",a[k]);} printf("\n");
}
Selection Sort
#include <stdio.h>
main()
{
int i,j,x,min,k;
int a[5]={5,3,2,4,1};
for(i=0;i<5;i++) {
min=i;
for(j=i+1;j<5;j++) {
if(a[min]>a[j]) {
min=j;
}
x=a[min];
a[min]=a[j];
a[j]=x;
} }
for(k=0;k<5;k++) {printf("%d ",a[k]);}
printf("\n");
}
*****************************************************************************************
The above codes are only for array with 5 elements in it. It will also work for "n "elements. Check it out for "n" elements.
Copyrights@Anil Kumar