Insertion Sort is simple sorting technique,used to compare the list of element by inserting the element between of two numbers.Here is the program for Insertion Sort In C using functions(recursively).
#include<stdio.h>
#include<conio.h>
void insertion(int [],int );
void main(){
int n,i,a[100];
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
insertion(a,n);
getch();
}
void insertion(int a[],int n){
int i,j,temp;
for(i=2;i<=n;i++){
temp=a[i];
j=i-1;
while(temp<a[j]&&j>=1){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("the sorted list\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
}
output:
No comments:
Post a Comment