Greetings to everyone!
As the topic title states, I want to implement merge sort, using child processes, but I can't get it right. My algorithm looks like this:
Code:
void merge(int arr[], int beg, int end, int pid1, int pid2)
{
int mid = (beg+end)/2, i = 0, *v, lo = beg, hi = mid+1;
if (beg>=end) return;
merge(arr, beg, mid); //this must be executed by a child process
merge(arr, mid+1, end); //this must be executed by another child process
//merge left and right - this must be executed after both child processes finish
v = (int *)malloc(sizeof(int)*(end-beg+11));
while ((lo<=mid)&&(hi<=end))
if (arr[lo]<arr[hi]) v[i++] = arr[lo++];
else v[i++] = arr[hi++];
if (lo<=mid)
for (hi = lo; hi <= mid; hi++) v[i++] = arr[hi];
else
if (hi<=end)
for (lo = hi; lo <= end; lo++) v[i++] = arr[lo];
for (i=beg, lo=0; i<=end; i++) arr[i] = v[lo++];
free(v);
}
How am I suppose to write this? May you help me?
Cheers.