Click here to Skip to main content
15,666,555 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

#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;
		int newnumber = 0;
		cout << "element(" << counter << ") = ";
		cin >> newnumber; getchar();
		if (cin.good())
		if ({
			cout << "numbers are :";
	} 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;
	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++)
		cout << "now returning sorted vector as\n";
	return one;

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

int main(){
	vector<int> numbers(getvector());	
	cout << "\nnumbers are :";

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,
Share this answer
You probably meant to write:
void mergesort(vector<int>& numbers)
		numbers = merge(halfone, halftwo);

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
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