我有一个大学的任务来编写一个程序,该程序接收一个整数数组及其大小,并返回一个指向原始数组值的排序数组。排序必须使用 mergeSort 算法来完成,我不允许触摸原始数组,只能更改指针数组。
谁能帮我吗?这是我的代码:
void sortedPointerArray (int **arr1[], int size1, int **arr2[], int size2, int **arr3[])
{
int read1, read2, writeind;
read1 = read2 = writeind = 0;
while( (read1<size1) && (read2 < size2))
{
if (*(arr1[read1]) < *(arr2[read2]))
{
arr3[writeind] = arr1[read1];
writeind++;
read1++;
}
else
{
arr3[writeind] = arr2[read2];
writeind++;
read2++;
}
}
while (read1 < size1)
{
arr3[writeind] = arr1[read1];
writeind++;
read1++;
}
while (read2 < size2)
{
arr3[writeind] = arr2[read2];
writeind++;
read2++;
}
}
void mergeSort (int **arr, int size)
{
int temp, i;
if(size <=1)
return;
else
{
mergeSort(arr, size/2);
mergeSort(arr+(size/2), size-(size/2));
int** temp = (int **)malloc(size*sizeof(int *));
sortedPointerArray(arr,size/2,arr+(size/2),size-(size/2),temp);
for (i=0; i<size; i++)
(*arr)[i] = (*temp)[i];
free(temp);
}
}
由于某些未知原因,我无法编译它并且它不起作用......任何帮助将不胜感激!