How can I make my Merge Sort Code In C more secure, effective and efficient I have been implementing the merge-sort algorithm for sorting the data for a long time, each time I use the following source code for implementation.
By secure, I mean, taking care of memory management, loopholes etc.

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;assert.h&gt;
void mergesort(int*,int,int);
void mergeSequence(int*,int,int,int); //Merge the two arrays
void displaySequence(int*);
int n;
int main()
{
    int t,test;
    int*num=NULL;
    int i;
    printf("Enter the number of test cases\n", test);
    scanf("%d", &amp;test);
    assert(test&gt;0);
    for(t=0;t&lt;test;t++)
    {
        printf("Enter the size of the sequence\n", n);
        scanf("%d", &amp;n);
        assert(n&gt;0);
        num=calloc(sizeof(int),n);
        assert(num);
        printf("Enter the sequence\n");
        for(i=0;i&lt;n;i++)
            scanf("%d", (num+i));
        printf("Sequence before sorting\n");
        displaySequence(num);
        mergeSort(num,0,(n-1));
        printf("Sequence after sorting\n");
        display(num);
        free(num);
    }
    return 0;
}
void displaySequence(int*num)
{
    int i;
    for(i=0;i&lt;n;i++)
        printf("%d ", num[i]);
    printf("\n");
}
void mergeSort(int*num,int start,int end)
{
    int mid;
    mid=start+((end-start)/2);
    printf("Mid-&gt;%d\n", mid);
    if(start&lt;end)
    {
        mergeSort(num,start,mid);
        mergeSort(num,(mid+1),end);
        mergeSequence(num,start,mid,end);
    }

}
void mergeSequence(int*num,int start,int mid,int end)
{
    int n1,n2;
    int i,j,k;
    n1=(mid-start)+1;
    n2=(end-mid);
    printf("N1-&gt;%d\nN2-&gt;%d\n", n1,n2);
    int left[n1];
    int right[n2];
    for(i=0;i&lt;n1;i++)
        left[i]=num[start+i];
    for(i=0;i&lt;n1;i++)
        printf("%d ", left[i]);
    printf("\n");
    for(j=0;j&lt;n2;j++)
        right[j]=num[(mid+1)+j];
    for(j=0;j&lt;n2;j++)
        printf("%d ", right[j]);
    printf("\n");
    //Now I have two arrays, and I can now merge them
    k=start;
    i=0;
    j=0;
    while((k&lt;=end))
    {
        if(i==n1)
        {
            num[k]=right[j];
            j++;
        }
        else if(j==n2)
        {
            num[k]=left[i];
            i++;
        }
        else if(left[i]&lt;=right[j])
        {
            num[k]=right[j];
            j=j+1;
        }
        else
        {
            num[k]=left[i];
            i=i+1;
        }
        k++;
    }
    displaySequence(num);
}
make the code more compact and secure


Separate sort code from test code

To well address security issues, OP needs to present a clear distinction between what is the sort code and what is not.  Put all sort code in a separate .c file.

int vs. size_t

C employs size_t as the type that is neither to narrow nor too wide for array indexing and sizing.

Although int is commonly sufficient, to make more secure and work with large data, use size_t.

Aside from changing int --> size_t and "%d" --> "%zu", a singular challenge is to remember size_t is some unsigned type and code needs to consider that.  (size_t)x &lt; 0 is never true.

Beware large VLAs

Security risk: int left[n1]; is subject to stack overflow.  Consider *alloc() instead.  Insufficient memory can be detected that way.

Avoid exposing helper functions

mergeSequence() is not meant to be used outside of mergesort().  To prevent other code from using this functions, make it static.  Further, put it and mergesort() in their own .c file.

This increases security as mergeSequence()  then not need to handle all sorts of arguments, just the ones mergesort() generates.

All warnings enabled?

Save time, enable all warnings.

I'd expect mergeSort(num,0,(n-1)); (upper case S) to warn about function usage before declaration.

void mergesort(int*,int,int); (lower case S) is declared but not used.

Avoid generic names when a specific type is required

mergeSort() sorts int.  With a name like mergeSort, I expected something like qsort() with its size-of-element parameter and compare function.

Of re-architect your merge sort code to take a generic object type.

void SS_mergeSort(void *base, size_t nmemb, size_t size, 
    int (*compar)(const void *, const void *));`


Compact by eliminating print out

It is unexpected that a sort routine would do any printing.  Remove printf() and displaySequence() from merge...() functions.

Compact left,right code

int left[n1]; int right[n2]; can be int left_right[n1+n2]; and then the copying facilitated with the fast lib function memcpy().

memcpy(left_right, num, sizeof left_right);
int *left = left_right;
int *right = left_right + n1;
...