Hi, I 'm working on this practice problem. In which I am testing my code using bash command with a text file. The file contains numbers the 1st number is the total of rest of the numbers.

For example 1st number as a 4 means there are total 4 numbers in line.

Example array length 8

3

5

6

7

0

4

1

2

Then I have to use merge sort and after that

the output is

First element is at subscript 4

0 3 5

1 5 2

2 6 3

3 7 -1

4 0 6

5 4 1

6 1 7

7 2 0

It works fine for indices column, input column, but not the links. I did my search on links I find out they can be extracted from sort function. Now my question is the mergesort functions I'm using are these proper for this purpose. What is missing?

**What I have tried:**
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int binary(int a[],int n,int m,int l,int u);
void partition(int **arr,int low,int high);
void mergeSort(int arr[],int low,int mid,int high);
int main()
{
int n, j, temp, i=0, low = 0, indx = 0;
int *index, *input, *link, *sorted;
scanf("%d ",&n);
input = (int*) malloc((n)* sizeof(int));
index = (int*) malloc((n)* sizeof(int));
link = (int*) malloc((n)* sizeof(int));
sorted = (int*) malloc((n)* sizeof(int));
if(input == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
for(i= 0; i < n; i++)
{
scanf("%d",&input[i]);
index[i]=i;
sorted[i]=input[i];
link[i]=-1;
}
partition(&sorted,0,n-1);
low=sorted[0];
for(i=0;i<n;i++){
if(input[i]==low)
{
indx=i;
}
}
for(i=0;i<n;i++){
temp= input[i];
for(j=0;j<n;j++){
if(sorted[j]==temp){
link[i]=j+1;
printf("print link j+i %d\n", link[j]);
if(link[i]==input[i]){
}
}
}
}
printf("First element is at the subscript %d \n", indx);
for(i=0;i<n;i++){
printf(" %d %d %d \n",index[i],input[i],link[i]);
}
free(index);
free(input);
free(link);
return 0;
}
void partition(int **arr,int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(*arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}