| | |
merge sort
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
I am trying to generate a random vector of integers and sort them using merge sort.
I am having trouble with the code. It is long, but I would appreciate if someone could take a look at it and see if the can help me, I've been working on it for days and am completely stuck.
When I run the program, it just stops at the point where I call the mergesort. I believe the problem may be in the merge split, because when I cout the values for low, high, and half there, I get 0 1 and 1, and this never changes. Thanks for the help.
I am having trouble with the code. It is long, but I would appreciate if someone could take a look at it and see if the can help me, I've been working on it for days and am completely stuck.
When I run the program, it just stops at the point where I call the mergesort. I believe the problem may be in the merge split, because when I cout the values for low, high, and half there, I get 0 1 and 1, and this never changes. Thanks for the help.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; vector<int> merge(vector<int> list1, vector<int> list2){ vector<int> result; int int1=0; int int2=0; cout << "merging lists" << endl; while(int1 < list1.size() || int2 < list2.size()) { if (int1 < list1.size() && int2< list2.size()) { if (list1[int1] < list2[int2]) result.push_back(list1[int1++]); else result.push_back(list2[int2++]); } else{ while(int1 < list1.size()) result.push_back(list1[int1++]); while(int2 < list2.size()) result.push_back(list2[int2++]); } } return result; } vector <int> merge_split(vector<int> list, int low, int high){ cout << "performing merge split" << endl; int half = ((high +1) - low)/2; cout << low << " " << high << " " << half << endl; if (high = low){ vector<int> res; res.push_back(list[low]); return res; } else if (high - low == 1){ vector <int> res; if(list[low] < list[high]){ res.push_back(list[low]); res.push_back(list[high]); } else { res.push_back(list[high]); res.push_back(list[low]); } } vector<int> list1 = merge_split(list, low, low + half); vector<int> list2 = merge_split(list, low + half + 1, high); return merge(list1, list2); } void merge_sort(vector<int> &list){ cout << "performing merge sort" << endl; int low = 0; int high = list.size() - 1; list = merge_split(list, low, high); return; } int main(){ int i; vector <int> list; for (i = 0; i < 20; i++){ list.push_back(rand()); } int size = 20; cout << "Before" << endl; for (i = 0; i < size; i++) cout << list[i] << " "; cout << endl; merge_sort(list); cout << "merge sort, after" << endl; cout << "After"; for(i = 0; i < size; i++){ cout << list[i] << " "; } cout << endl; return 0; }
You need to pass those vectors by reference instead of by value to avoid duplicating the vectors and so that the changes will be available to the calling function
vector<int> merge(vector<int>& list1, vector<int>& list2){ Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Merge Sort on a Array-Based List (C++)
- Merge sort not working (C++)
- Merge Sort (C)
- hw assignment help on selection & merge sort (C)
- another merge sort question (C++)
- Merge sort (C++)
Other Threads in the C++ Forum
- Previous Thread: encrypting text
- Next Thread: matrix prog.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






