So, I have been trying to understand how to do recursive merge sort using a wrapper function. But using the Wrapper function and W( see code), how do I write the merge function to actually combine the arrays to get the solution?
//Recursive mergesort in C
#include<stdio.h>
#include<stdlib.h>
void Srec(int *A, int n, int* W);
void Msort(int *A, int n);
void merge(){
//How to write this?
}
void Srec(int *A, int n, int* W){ //Function to recursively sort the arrays
if(n<=1){
return;
}
Srec(A,n/2,W); //First half
Srec((A+(n/2)),(n-(n/2)),W); // second half
merge(W,n,A); // Here is where I called merge...
}
void Msort(int *A, int n){ // Wrapper function
int* W = (int*) calloc(20,sizeof(int)); // Creates dyn. array with size 20
Srec(A,n,W);
free(W);
}
int main(int argc, char const *argv[])
{
int* A = (int*) calloc(20,sizeof(int));
int B[] = {1,3,2,4,7,6,5,0,9,10};
A = B;
int n = 10;
Msort(A,n);
return 0;
}