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. - #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;
-
}
5 4480
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?
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?
Oh, I see what you mean now, thank you very much
Unfortunately, that was not the only problem though. It still doesnt work.
Well, you definitely need to rethink your merge( list, list ) logic. That code is definitely wrong.
Max
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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));
|
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;
| |
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...
|
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)
|
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)
|
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
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |