有人能告诉我下面的mergesort实现有什么问题吗?我已经挠了好几个小时了。。
void merge(int arr[], int low, int mid, int high)
{
int i = 0;
int j = 0;
int k = 0;
int buf1[1024];
int buf1Length = mid - low + 1;
int buf2[1024];
int buf2Length = high - (mid + 1) + 1;
for (i = low; i <= mid; i++)
{
buf1[j++] = arr[i];
}
for (i = mid + 1; i <= high; i++)
{
buf2[k++] = arr[i];
}
i = 0;
j = 0;
k = 0;
while (j < buf1Length && k < buf2Length)
{
if (buf1[j] <= buf2[k])
{
arr[i++] = buf1[j++];
}
else
{
arr[i++] = buf2[k++];
}
}
while (j < buf1Length)
arr[i++] = buf1[j++];
while (k < buf2Length)
arr[i++] = buf2[k++];
}
void mergeSort(int arr[], int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
int main()
{
int i;
int arr[] = {6, 7, 2, 4, 9, 8};
mergeSort(arr, 0, 5);
printf("\n\n results: \n");
for (i = 0; i < 6; i++)
{
printf("%d ", arr[i]);
}
printf("\n\n\n");
return 0;
}
最佳答案
您确定要执行以下操作:
i = 0;
j = 0;
k = 0;
?
乍一看,我认为应该是:
i = low;
j = 0;
k = 0;
关于c - 有人可以告诉我我的mergesort怎么了吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4988508/