473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

merge sort

14 New Member
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 4501
mschenkelberg
44 New Member
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
Sarahger9
14 New Member
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
Sarahger9
14 New Member
Oh, I see what you mean now, thank you very much
Dec 3 '07 #4
Sarahger9
14 New Member
Unfortunately, that was not the only problem though. It still doesnt work.
Dec 3 '07 #5
mschenkelberg
44 New Member
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
4607
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 comparisons. It seems to me like I should just use a single counter in the merge function's 'if' statement, but this can't be right because an array of 50 takes about 100 comparisons this way. If anyone has any suggestions I would greatly...
1
12848
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
3551
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 integers in an ascending order, which probably isn't very useful on its own. I know the code works for a reasonable and arbitrary size of input, but
9
5629
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
13
4043
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 pointer to the array, size is # of elements { int b;
7
14260
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#, but I tried to develop a merge sort program myself, but have got stuck with a Null reference exception for the pos variable in merge function. Also I wrote a function to get the actual nodes from teh index values before calling merge function. Is...
16
3610
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 so it runs for ever. I checked the table and PK statistics of the big table and it looks good. Please let me know if I need to check for something else. Here are more details Small table - Non Partitioned ( Node 0)
2
2894
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 am doing wrong? void merge(int,int,int); void merge_sort(int low, int high) { int mid, temp; if (low==high) return; if (low+1==high)
9
9033
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 and re-sort. The following test case demonstrates this. It can be criticized in many ways: it only tests lists of the same size, it only uses "hashed" data, etcetera... Still, my testing shows "resort everything" is consistently
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.