49)Write a C program to store an array element using insertion sort method.


#include<stdio.h>
#include<conio.h>
#define MAX 20
void insertsort(int [MAX],int);
void display(int [MAX],int);
void main()
{
int a[20],i,n;
clrscr();
printf(“\nHow many numbers:”);
scanf(“%d”,&n);
printf(“\nEnter the unsorted element: “);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
insertsort(a,n);
printf(“\n\nThe Sorted list is: “);
display(a,n);
getch();
}
void insertsort(int a[MAX],int n)
{
int i,j,key;
printf(“\nstep \t Sorted list”);
for(i=1;i<n;i++)
{
key=a[i];
printf(“\n\n%d”,i);
display(a,i);
for(j=i-1;j>=0 && a[j]>key;j–)
// j=i-1;
// while((j>=0) && (a[j]>key))
{
a[j+1]=a[j];
// j=j-1;
}
a[j+1]=key;
}
}
void display(int a[MAX],int n)
{
int i;
for(i=0;i<n;i++)
printf(“\t%d”,a[i]);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s