Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Thursday, 24 October 2013

[C Program] To Perform Insertion Sort

//Program for insertion sort

#include<stdio.h>
#include<conio.h>
void insertion(int [], int size);
int main()
{
int a[30],i,size;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter elements:");
for(i=0; i<size; i++)
{

scanf("%d",&a[i]);
}
insertion(a,size);
printf("\nSorted elements:\n\n");
for(i=0; i<size; i++)
printf(" %d",a[i]);
getch();
return 0;
}

void insertion(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n;i++)
{

for(j=i-1;j>-1;j--)
{

if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
else
break;
}
}
}

/*Output of Program:

Enter the array size:6

Enter the elements into array:31  56  80  111  65  71

Entered array:  31      56      80      111     65      71

Sorted list:       31      56      65      71      80      111*/
Share it Please

Pankaj Gaikar

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine