Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've written the code below as a merge sort using vectors and it has a problem.
I pass the vectors by reference and change them inside the called function and when the caller function uses the vector it's still the same as I entered those numbers into the vectors without any changes.

C++
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
void showvector(vector<int> numbers){
	for (vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
	{
		cout << *i;
		if (i != numbers.end() - 1)cout << " - ";
	}
}

vector<int> getvector(){
	vector<int> numbers(0);
	cout << "please enter you numbers :::\n''entering any characters but numbers is the end of entry''\n";
	int counter = 0;
	do{
		int newnumber = 0;
		cout << "element(" << counter << ") = ";
		counter++;		
		cin >> newnumber; getchar();
		if (cin.good())
			numbers.push_back(newnumber);
		if (cin.fail()){
			cout << "numbers are :";
			showvector(numbers);
		}
	} while (cin.good()); getchar();
	return numbers;
}

vector<int> merge(vector<int>& one, vector<int>& two){
	cout << "\ncomparing vector one with "; showvector(one); cout << " element(s) with vector two with "; showvector(two); cout << " element(s)\n";
	vector<int>::iterator j = two.begin();
	for (vector<int>::iterator i = one.begin(); i != one.end(); i++){
		cout << "now comparing " << *i << " with " << *j<<endl; getchar();
		if (*i > *j){
			cout << "now exchanging " << *i << " with " << *j << endl;;
			int c = *i;
			*i = *j;
			*j = c;
			j++;
		}
	}
	cout << "pushing vector two with "; showvector(two); cout << " element(s) back to vector one with "; showvector(one); cout << " element(s)\n";
		for (j=two.begin(); j != two.end();j++)
			one.push_back(*j);
		cout << "now returning sorted vector as\n";
	showvector(one);
	return one;
}

void mergesort(vector<int>& numbers){
	if (numbers.size() > 1){		
		vector<int> halfone(numbers.begin(), numbers.begin() + numbers.size() / 2);
		mergesort(halfone);
		vector<int> halftwo(numbers.begin() + numbers.size() / 2, numbers.end());		
		mergesort(halftwo);
		merge(halfone, halftwo);
	}			
}

int main(){
	vector<int> numbers(getvector());	
	mergesort(numbers);
	cout << "\nnumbers are :";
	showvector(numbers);
	getchar();
}
Posted

That's because you create a new vectors in mergesort.

In this function;
void mergesort(vector<int>& numbers)

you're not modifying the contents of numbers, you're creating two new vectors, copying the elements from numbers over to the new vectors.

This is why the result is the same as the input.

Hope this helps,
Fredrik
 
Share this answer
 
You probably meant to write:
C++
void mergesort(vector<int>& numbers)
{
        {
                ...
		numbers = merge(halfone, halftwo);
	}			
}
</int>

But let me tell you that copying those vectors back an forth will make it a really inefficient way of sorting. Instead of copying vectors you better pass pointers and element counts to your merge function and let it sort the elements in-place.
 
Share this answer
 
Comments
m.r.m.40 30-Jan-15 13:13pm    
Yes, exactly.
As it's an educational programming, after I'm done with the algorithm, I'll definitely follow your suggestion about using pointers instead of copying vectors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900