I've been trying to get a merge sort to fix character arrays for a week now, and I cannot figure out for the life of me why I am getting half way sorted and then it just doubles everything. Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <sys/timeb.h>
using namespace std;
void MyMergeSort(int sourceArray[], int destinationArray[], int low, int high)
{
if (low != high)
{
int middle = (low+high)/2;
MyMergeSort(destinationArray, sourceArray, low, middle);
MyMergeSort(destinationArray, sourceArray, middle+1, high);
cout << "Merged: " << low << ", " << middle << " with " << middle+1 << ", " << high << endl;
int start1 = low;
int end1 = middle;
int start2 = middle+1;
int end2 = high;
int destinationCount = 0;
while ((start1 <= end1) && (start2 <= end2))
{
if(sourceArray[start1] < sourceArray[start2])
{
destinationArray[destinationCount] = sourceArray[start1];
start1++;
}
else
{
destinationArray[destinationCount] = sourceArray[start2];
start2++;
}
destinationCount++;
}
for(; start1 <= end1; start1++, destinationCount++)
{
destinationArray[destinationCount] = sourceArray[start1];
cout << "in for loop start1" << endl;
}
for(; start2 <= end2; start2++, destinationCount++)
{
destinationArray[destinationCount] = sourceArray[start2];
cout << "in for loop start2" << endl;
}
}
}
void main()
{
int samplearray[10] ={10, 20, 30, 40, 50, 5, 6, 7, 35, 38};
int copyarray[10];
for (int x = 0; x < 10; x++)
{
copyarray[x] = samplearray[x];
}
MyMergeSort(copyarray, samplearray, 0, 9);
cout << "Sample Array Contents:" << endl;
for (int x = 0; x < 10; x++)
{
cout << samplearray[x] << " ";
};
cout << endl << endl;
cout << "Copy Array Contents:" << endl;
for (int x = 0; x < 10; x++)
{
cout << copyarray[x] << " ";
};
for (int z=start;z<=finish;z++)
{
cout << MyArray[z] << " ";
}
*/
int hold;
cin >> hold;
}