473,379 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

merge sort

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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> merge(vector<int> list1, vector<int> list2){
  6.     vector<int> result;
  7.     int int1=0;
  8.     int int2=0;
  9.     cout << "merging lists" << endl;
  10.     while(int1 < list1.size() || int2 < list2.size())
  11.     {
  12.         if (int1 < list1.size() && int2< list2.size())
  13.         {
  14.             if (list1[int1] < list2[int2])
  15.                     result.push_back(list1[int1++]);
  16.                 else 
  17.                     result.push_back(list2[int2++]);
  18.             }
  19.             else{
  20.                 while(int1 < list1.size())
  21.                     result.push_back(list1[int1++]);
  22.                 while(int2 < list2.size())
  23.                     result.push_back(list2[int2++]);
  24.             }
  25.         }
  26.         return result;
  27.     }
  28.  
  29. vector <int> merge_split(vector<int> list, int low, int high){
  30.     cout << "performing merge split" << endl;
  31.     int half = ((high +1) - low)/2;
  32.     cout << low << "  " << high << "  " << half << endl;
  33.     if (high = low){
  34.         vector<int> res;
  35.         res.push_back(list[low]);
  36.         return res;
  37.     }
  38.     else if (high - low == 1){
  39.         vector <int> res;
  40.         if(list[low] < list[high]){
  41.             res.push_back(list[low]);
  42.             res.push_back(list[high]);
  43.         }
  44.         else {
  45.             res.push_back(list[high]);
  46.             res.push_back(list[low]);
  47.         }
  48.     }
  49.         vector<int> list1 = merge_split(list, low, low + half);
  50.         vector<int> list2 = merge_split(list, low + half + 1, high);
  51.         return merge(list1, list2);
  52. }
  53.  
  54. void merge_sort(vector<int> &list){
  55.     cout << "performing merge sort" << endl;
  56.     int low = 0;
  57.     int high = list.size() - 1;
  58.     list = merge_split(list, low, high);
  59.     return;
  60. }
  61.  
  62. int main(){
  63.     int i;
  64.     vector <int> list;
  65.     for (i = 0; i < 20; i++){
  66.         list.push_back(rand());
  67.     }
  68.     int size = 20;
  69.     cout << "Before" << endl;
  70.     for (i = 0; i < size; i++)
  71.         cout << list[i] << "  ";
  72.     cout << endl;
  73.     merge_sort(list);
  74.     cout << "merge sort, after" << endl;
  75.     cout << "After";
  76.     for(i = 0; i < size; i++){
  77.         cout << list[i] << "  ";
  78.     }
  79.     cout << endl;
  80.     return 0;
  81. }
Dec 3 '07 #1
5 4453
if ( high = low )
{
....
}

The first thing you are doing in merge_split is storing the value of low in high which will then evaluate to false every time... bad!

I'm surprised you didn't get a compiler warning for that code. What compiler are you using?
Dec 3 '07 #2
I'm using visual basic. It compiled, but wouldn't complete the code, it would just break off.

But I am having trouble with what you are saying, how should I go about declaring the low and high values?
Dec 3 '07 #3
Oh, I see what you mean now, thank you very much
Dec 3 '07 #4
Unfortunately, that was not the only problem though. It still doesnt work.
Dec 3 '07 #5
Well, you definitely need to rethink your merge( list, list ) logic. That code is definitely wrong.

Max
Dec 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
5
by: uppe | last post by:
Hey everyone, I've just finished my implementation of the merge-sort algorithm in C, and I thought I could ask for some feedback. (One can always improve, they say) Right now, the code sorts...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
13
by: ralphedge | last post by:
These sorts work fine on 100000 ints but if I go much higher they will both segmentation fault **************************MERGESORT********************* mergesort(int *a, int size) //a is...
7
by: Zeba | last post by:
Hi, I have to write program in C# to merge sort a linked list (doubly linked). Is it true that the best way to do it is copy it into an array, sort it and then convert back ? I'm new to C#,...
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
2
by: mqueene7 | last post by:
below is my code for my merge sort but I can't get it to sort properly. I am trying generate random numbers based on input from the user and then sort those random numbers. Can you tell me what I...
9
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.