EmbLogic's Blog

Insertion Sorting

#include<stdio.h>
//Program for Insertion Sorting
int main()
{
int i,j,k,N,a[100],temp;
printf(“Enter no. of elements you want to sort :\t”);
scanf(“%d”,&N);//Max. no. of elements
printf(“Enter %d elements you want to sort :\t”,N);
for(i=0;i<N;i++)
scanf(“%d”,(a+i));
printf(“\nBefore Sorting:\t”);
for(i=0;i<N;i++)
printf(“%d\t”,*(a+i));
//Sorting Process Start
for(i=1;i<N;i++)
for(j=0;j<i;j++)
if(a[i]<a[j])
{
temp = a[i];
for(k=i;k>j;k–)
{
a[k]=a[k-1];
}
a[j]=temp;
}

//Sorting Ends
printf(“\nAfter Sorting:\t”);
for(i=0;i<N;i++)
printf(“%d\t”,*(a+i));
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>