TO SORT THE GIVEN LIST OF ELEMENTS USING INSERTION SORT

//TO SORT THE GIVEN LIST OF ELEMENTS USING INSERTION SORT//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,k,ptr,temp,A[10];
clrscr();
printf(“Enter the size of the array:”);
scanf(“%d”,&n);
printf(“Enter the elements in the array:\n”);
for(i=1;i<=n;i++)
{
printf(“Element number %d is “,i);
scanf(“%d”,&A[i]);
}
//SORTING BEGINS//
A[0]=-1;
for(k=2;k<=n;k++)
{
temp=A[k];
ptr=k-1;
while(temp<A[ptr])
{
A[ptr+1]=A[ptr];
ptr=ptr-1;
}
A[ptr+1]=temp;
}       //SORTING ENDS//
printf(“Sorted array is:\n”);
for(i=1;i<=n;i++)
{
printf(” Element number [%d] is %d\n”,i,A[i]);
}
getch();
}

Leave a Reply

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