Sorted Array Insertion: Given a sorted array, insert an element such that array remains sorted

Sorted Array Insertion: Given a sorted array, insert an element such that array remains sorted


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

/* Insertion sort */

void insertionsort(int *a,int n,int x)
    {
    int hole;
    hole=n;
    while(hole>0 && *(a+hole-1)>x)
        {
        *(a+hole)=*(a+hole-1);
        hole=hole-1;
    }
    *(a+hole)=x;
}

int main() {

    int T,N,i,*a,x;
    a=(int*)malloc(sizeof(int)*1000);
    scanf("%d",&T);
    while(T--)
        {
        scanf("%d",&N);
        for(i=0;i<N;i++)
            scanf("%d",(a+i));
        scanf("%d",&x);
        insertionsort(a,N,x);
        for(i=0;i<=N;i++)
            printf("%d ",*(a+i));
        printf("\n");
    }
    return 0;
}
/* Method 2*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int T,N,i,*a,x,flag;
    a=(int*)malloc(sizeof(int)*1000);
    scanf("%d",&T);
    while(T--)
        {
        scanf("%d",&N);
        flag=1;
        for(i=0;i<N;i++)
            scanf("%d",(a+i));
        scanf("%d",&x);
        for(i=0;i<N;i++)
            {
           if(*(a+i)<x)
               printf("%d ",*(a+i));
            else
                {
                printf("%d ",x);
                break;
            }
    }
        for(;i<N;i++)
            printf("%d ",*(a+i));
        printf("\n");
    }
    return 0;
}