#include <stdio.h>
#include <conio.h>
int main()
{
int a[6] = {5,2,4,6,1,3};
int j,i,k,cnt=1;
for (j=1;j<7;j++)
{
k = a[j];
i = j-1;
while(i>0 && a[i]>k)
{
a[i+1] = a[i];
i = i-1;
}
a[i+1] = k;
}
for(i=0;i<6;i++)
{
printf("\n Final op %d",a[i]);
}
getch();
}
I am passing some elements in a static array but the 1st element that’s value
a[0] is not getting sorted but rest of the values from a[1] to a[5] gets sorted
make only this change and your insertion sort will work perfectly.
its not getting sorted because array starts from a[0] n your while gets terminated when its at a[0] so not visiting its element.
hope it helped you