Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 URGENT: Merge Sort for Vectors Not Sorting...
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MadiAraly




PostPosted: Thu Jun 09, 2016 9:38 pm   Post subject: URGENT: Merge Sort for Vectors Not Sorting...

Hi there. I'm new to the merge sort, but this is day four and I can't for the life of me figure out why this code won't sort my vector. I use CodeBlocks.
When compiled, it simply outputs the array it it's scattered order. I don't understand why it executes the function, yet apparently does nothing at the same time. ANY help is appreciated, thank you so much!

#include <iostream>
#include <vector>
#include <conio.h>
#include <time.h>
#include <stdlib.h>//random number gen
using namespace std;

vector <int> c;

void merge(vector <int>,int, int , int );
void mergesort(vector <int> a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(vector <int> a, int low, int high, int mid)
{
int i, j, k;
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
srand (time(NULL));
vector <int> a;
vector<int> b;
for (int i=0; i<10000; i++)
c.push_back(0);
int i;
for (int i=0; i<10000; i++)
a.push_back(rand()%2147483647);
mergesort(a, 0, 9999);
cout<<"This is not okay. Is it actually done the merge func? What has happened...\n";
getch();
cout<<"sorted array\n";
for (i = 0; i < 10000; i++)
{
cout<<c[i]<<endl;//NOTE that I keep changing this from 'c' to 'a' because I wanted to see if it sorted either. It didn't...
}
getch();
}
Canada
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Jun 10, 2016 8:41 am   Post subject: RE:URGENT: Merge Sort for Vectors Not Sorting...

You are modifying a copy of your vector.

When you declare a function that takes some_thing, that object is passed by value, which means as a copy. Example:

c++:

void increment(int i){
  i++;
}
int main() {
  int x = 3;
  increment(x);
  cout << x << endl; // outputs 3, not 4
}


If you take a pointer or a reference (or const pointer, or const reference, or ...) then you will be working on the original, and there will be no copy. This can be important for logically-correct programs (as you've seen) as well as performance (which is much murkier, but can often be better if you don't copy things).

Further reading: http://stackoverflow.com/questions/26647152/passing-vectors-to-a-function-value-vs-reference-c
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: