You are on page 1of 2

Code:-

#include<stdio.h>
#include<conio.h>
#define max 10
void insertion_sort(int[],int);
int main()
{
int arr[max],i,size;
do
{
//clrscr();
printf("\nEnter the size of the array:");
scanf("%d",&size);
if(size>max)
{
printf("\ninput size is greater than the maximum size.");
getch();
}
}while(size>max);
printf("\nEnter the elements of the array:\n");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
insertion_sort(arr, size);
printf("\nthe sorted array is:");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
getch();
}
void insertion_sort(int arr[], int size)
{
int i,j,k,temp;
for(i=1;i<size;i++)
{
temp=arr[i];
j=i-1;
while(temp<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
printf("\n array after comparion %d: ",i);
for(k=0;k<size;k++)
{
printf("%d ",arr[k]);
}
}
printf("\n No. of comparion: %d ", i-1);
}

OUTPUT-

You might also like