Merge sort using sentinels This is my implementation of recursive merge sort, using sentinels:

    #include&lt;stdio.h&gt;
    #include&lt;stdlib.h&gt;
    #include&lt;limits.h&gt;

void merge(int a[], int p, int q, int r)
{
    int n1,n2;
    int i,j,k;
    int *l,*m;

    n1 = q - p + 1;
    n2 = r - q;
    l = (int*)malloc(sizeof(int)*(n1+1));
    m = (int*)malloc(sizeof(int)*(n2+1));

    for(i=0; i&lt;n1; i++)
        l[i] = a[p+i];
    for(j=0; j&lt;n2; j++)
            m[j] = a[q+j+1];
    l[i] = INT_MAX;
    m[j] = INT_MAX;

    i = j = 0;
    for(k=p; k&lt;r+1; k++){

        if(l[i] &lt;= m[j]){
            a[k] = l[i];
            i++;
        }

        else{
            a[k] = m[j];
            j++;
        }

    }
}

void merge_sort(int a[],int p, int r)
{
    int q;

    if(p&lt;r){
        q = (p+r)/2;
        merge_sort(a,p,q);
        merge_sort(a,q+1,r);
        merge(a,p,q,r);
    }
}
int main()
{
    int *num,n;
    int i;

    printf("Enter number of digits:");
    scanf("%d",&amp;n);
    num = (int*)malloc(sizeof(int)*n);
    printf("Enter numbers:");   
    for(i=0 ; i&lt;n; i++){
        scanf("%d",&amp;num[i]);
    }
    merge_sort(num,0,n-1);
    printf("Sorted array:\n");
    for(i=0; i&lt;n; i++)
        printf("%d ",num[i]);
    free(num);
    return 0;
}


I'm looking for reviews, suggestions, and improvements.
In merge(), you call malloc() twice with no corresponding calls to free().  That can't be good for your memory consumption!

When computing the average of two values, don't do q = (p+r)/2, because that is vulnerable to overflow.  Instead, write it as q = p + (r - p) / 2.