Insertion Sort

#include<stdio.h>

#include<conio.h>

void insertion_sort(int [],int);

int a[100],i,j,n,temp;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

insertion_sort(a,n);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}


void insertion_sort(int a[],int n){

for(i=1;i<n;i++){

temp=a[i];

j=i-1;

while(j>=0 && a[j]>temp){

a[j+1]=a[j];

j--;

}

a[j+1]=temp;

}

}



No comments:

Post a Comment