```
#include <stdio.h>
#include <stdlib.h>

int main()
{

    int i, j, key, p;
    int  A[] = {1, 3, 2, 4, 9, 8, 7};

    for (j=1; j<7; j++){

        key = A[j];

    //insert A[j] into the sorted sequence 

        i = j-1;
        while(i>0 && A[i]>key){
            A[i+1] = A[i];
            --i;
        }

        A[i+i] = key;

   }
   for(i=0; i<7; ++i){

       printf("%d ", A[i]);
   }

   return 0;
}

```

The above stated code is supposed to perform an insertion sort, however, I am getting undesirable sort with few repeated values. I can't seem to figure out where I am going wrong.