I currently have this code that performs Merge sort (not completed yet) and I was wondering if anyone knows how to do it without recursion. the part I am stuck at is the part where I have to keep on creating new sub-arrays after each split. For example, if I have an array of length 8:
I would need 2 arrays for the initial split into 4 and 4 and then when I would have to split again into 2 2 2 2 which means I need 4 more arrays.. and so on. How do I accomplish this without recursion?
So far I got to the first split in my code:
void merge_sort(int arr[], int len) // len = 10, array of 10
{
int i;
int *firsthalf;
int *secondhalf;
int firsthalf_elements, secondhalf_elements;
if (len<2)
{
return;
}
firsthalf_elements = len/2;
secondhalf_elements = len - firsthalf_elements;
firsthalf = (int*)malloc(sizeof(int) * n1);
secondhalf = (int*)malloc(sizeof(int) * n2);
for (i =0; i < firsthalf_elements; i++)
{
firsthalf[i] = arr[i];
}
for (i = 0; i < secondhalf_elements; i++)
{
secondhalf[i] = arr[i+firsthalf_elements];
}
//Normally over here we would make a recursive call, but i want to do this w/o recursion.