I'm trying to code a merge sort in c++, but building it gives me a warning that it's recursive, and running it gives a stack overflow. I don't understand where I'm doing wrong.
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);
}
If you want to handle different parts of int []
by calling the same function, try to define your function as
void mergesort(int thelist[], int start, int end)
Regarding the sizeof(thelist)
part, if you want to get the size of a int []
. You need to use
sizeof(thelist)
instead of
sizeof(thelist)/sizeof(int)
Example:
int a[] = {1, 2, 3, 4, 5}; int n = sizeof(a)/sizeof(int); // n=5 now
PS: You also need to think twice on your code logic and rewrite it. You main function should looks like the following:
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);
}
}
#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;
}
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.