Solution:
#include<stdio.h>
void insertionsort(int [],int);
void main()
{
int A[30],i,j,n,temp;
printf("Enter size of array :\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
insertionsort(A,n);
printf("Sorted Array :\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
}
void insertionsort(int A[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=A[i];
j=i-1;
while(temp<A[j] && j>=0)
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=temp;
}
}
0 Comments