Original Post
I'm having trouble getting my merge sort to work. I get runtime errors. Not sure where I went wrong. I'm pretty sure it might have something to do with this little piece of code... But again, I'm not sure. If you want to run the whole program and see the runtime errors, <a href="http://paste.lisp.org/display/12966>the code is pasted here.
// ******
// Merge
// ******
static Data[] Merge(Data array[])
{
int array_size = array.length;
Data[] temp = new Data[array_size];
doMergeSort(array, temp, 0, array_size - 1);
return array;
}
static void doMergeSort(Data array[], Data temp[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
doMergeSort(array, temp, left, mid);
doMergeSort(array, temp, mid+1, right);
array = merge(array, temp, left, mid+1, right);
}
}
static Data[] merge(Data array[], Data temp[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if((array.name.compareTo(array[mid].name) < 0) || (array.name.compareTo(array[mid].name) == 0))
{
temp[tmp_pos] = array;
tmp_pos = tmp_pos + 1;
left = left + 1;
}
else
{
temp[tmp_pos] = array[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)
{
temp[tmp_pos] = array;
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
temp[tmp_pos] = array[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i=0; i <= num_elements; i++)
{
array = temp;
right = right - 1;
}
return array;
}
if((array.name.compareTo(array[mid].name) < 0) || (array.name.compareTo(array[mid].name) == 0))