我正在尝试在c ++中编写合并排序,但是构建它会给我一个警告,它是递归的,并且运行它会产生堆栈溢出。我不明白我在哪里做错了。
void mergesort(int thelist[]) {
if (sizeof(thelist) <= 1)
return;
int middle = (sizeof(thelist) / 2);
for (i = 0; i <= sizeof(thelist); i++){
if (i < middle)
lft[i] = thelist[i];
else if (i >= middle)
rght[i-middle] = thelist[i];
}
mergesort(lft);
mergesort(rght);
merge(lft, lft + 10, rght, rght + 10, sortedlist);
}
答案 0 :(得分:1)
如果您想通过调用相同的函数来处理int []
的不同部分,请尝试将您的函数定义为
void mergesort(int thelist[], int start, int end)
关于sizeof(thelist)
部分,如果您想获得int []
的大小。你需要使用
sizeof(thelist)
而不是
sizeof(thelist)/sizeof(int)
示例:
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a)/sizeof(int); // n=5 now
PS:您还需要对代码逻辑进行三思并重写它。您的主要功能应如下所示:
void MergeSort(int data[], int start, int end)
{
if (start < end)
{
int middle = (start+end)/2;
// sort for first part
MergeSort(data, start, middle);
// sort for second part
MergeSort(data, middle+1, end);
// merge both parts together
Merge(data, start, middle, end);
}
}
答案 1 :(得分:0)
#include <iostream>
using namespace std;
void merge(int *, int *, int, int, int);
void mergesort(int *a, int *b, int low, int high)
{
int pivot;
if (low < high)
{
pivot = (low + high)/2;
mergesort(a, b, low, pivot);
mergesort(a, b, pivot + 1, high);
merge(a, b, low, pivot, high);
}
}
void merge(int *a, int *b, int low, int pivot, int high)
{
int h, i, j, k;
h = low;
i = low;
j = pivot + 1;
while ((h <= pivot) && (j <= high)) // Traverse both halves of the array
{
if (a[h] <= a[j]) // if an element of left half is less than element of right half
{
b[i] = a[h]; // store element of left half in the temporary array
h++; // shift the index of the array from which the element was copied to temporary
}
else // otherwise store the element of the right half in the temporary array
{
b[i] = a[j];
j++; // shift the index of the array from which the element was copied to temporary
}
i++;
}
if (h > pivot) // If traversal of left half is done,
// copy remaining elements from right half to temporary
{
for (k = j; k <= high; k++)
{
b[i] = a[k];
i++;
}
}
else // otherwise copy remaining elements from left half to temporary
{
for (k = h; k <= pivot; k++)
{
b[i] = a[k];
i++;
}
}
for (k = low; k <= high; k++) a[k] = b[k]; // recopy the values from temporary to original array.
}
int main()
{
int a[] = {12, 10, 43, 23, -78, 45, 123, 56, 98, 41, 90, 24};
int num;
num = sizeof(a)/sizeof(int);
int b[num]; // temporary array to be used for merging
mergesort(a, b, 0, num-1);
for (int i = 0; i < num; i++)
cout << a[i] << " ";
cout << endl;
}
答案 2 :(得分:0)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_arr_data(std::vector<int>& arr, int start, int end) {
cout << "start: " << start << " end: " << end << endl;
int len = abs(end - start) + 1;
for(auto it = arr.begin() + start; it != arr.begin() + start + len; ++it) {
cout << *it << ",";
}
cout << endl;
}
void merge(std::vector<int>& arr, int start, int end) {
int len = abs(start - end);
std::sort(arr.begin() + start, arr.begin() + start + len);
}
void merge_sort(std::vector<int>& arr, int start, int end) {
// check if the array is not a unit array, then dont divide anymore
print_arr_data(arr, start, end);
if (start < end) {
int mid = (start + end)/2;
merge_sort(arr, start, mid);
merge_sort(arr, mid+1, end);
merge(arr, start, end);
}
}
int main() {
std::vector<int> data = {4,1,2,3,7,8,5,6,9};
merge_sort(data, 0, data.size()-1);
cout << "*******" << endl;
for (int i : data) {
cout << i << endl;
}
cout << "*******";
return 0;
}