C Program to implement Insertion sort


Simple C Program to implement Insertion Sort Algorithm


Code:


#include<stdio.h>



int main()

{

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

printf("Enter the number of elements:");

scanf("%d",&n);

printf("\nEnter the elements\n");



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

{

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

}



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

{

temp=a[i];

j=i-1;



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

{

a[j+1]=a[j]; //moves element forward

j=j-1;

}



a[j+1]=temp; //insert element in proper place

}



printf("\nSorted list is as follows\n");

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

{

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

}



return 0;

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Servlet Program to Print Today’s Date and Time using refresh header

Java Program to Implement sorting algorithm using TCP on Server application