Click here to Skip to main content
15,888,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
<pre>#pragma once
#include <vector>

template<typename T>
void MergeSort(std::vector<T>& vArray, std::vector<T>& vHelper, size_t iLow, size_t iHigh);

template<typename T>
void Merge(std::vector<T>& vArray, std::vector<T>& vHelper, size_t iLow, size_t iMiddle, size_t iHigh);

template<typename T>
void MergeSort(std::vector<T> &vArray)
{
	std::vector<T> vHelper(vArray.size());
	MergeSort(vArray, vHelper, 0, vArray.size() - 1);
}

template<typename T>
void MergeSort(std::vector<T> &vArray,std::vector<T> &vHelper,size_t iLow,size_t iHigh)
{
	if(iLow < iHigh)
	{
		size_t iMiddle = (iLow + iHigh) / 2;

		MergeSort(vArray,vHelper, iLow, iMiddle);//sort left half
		MergeSort(vArray,vHelper, iMiddle + 1, iHigh);//sort right half
		Merge(vArray, vHelper, iLow, iMiddle, iHigh);//merger both halves
	}
}

template<typename T>
void Merge(std::vector<T>& vArray, std::vector<T>& vHelper, size_t iLow, size_t iMiddle, size_t iHigh)
{
	//copy both halves into the helper array
	for (size_t i = iLow; i <= iHigh; i++)
	{
		vHelper[i] = vArray[i];
	}

	size_t iHelperLeft = iLow;
	size_t iHelperRight = iMiddle + 1;
	size_t iCurrent = iLow;

	/*Iterate throght helper array. 
	Compaeres the left half to the right half and copy back the smaller element into the original array*/

	while (iHelperLeft <= iMiddle && iHelperRight <= iHigh)
	{
		if (vHelper[iHelperLeft] < vHelper[iHelperRight])
		{
			vArray[iCurrent] = vHelper[iHelperLeft];
			iHelperLeft++;
		}
		else
		{
			vArray[iCurrent] = vHelper[iHelperRight];
			iHelperRight++;
		}
		iCurrent++;
	}

	//copy the remaining of the left sideof the helper array
	size_t iRemaining = iMiddle - iHelperLeft;

	
	for (size_t i = 0; i <= iRemaining; i++)
	{
		
		
		vArray[iCurrent + i] = vHelper[iHelperLeft + i];//one of these vectors go out of range
	}

}




What I have tried:

I have tried to debug it and have googled to no avail.
Posted
Updated 5-Aug-23 2:18am
Comments
Richard MacCutchan 5-Aug-23 7:28am    
You will not solve problems like this by means of google searches, but by using the debugger to find out what is actually happening when the error occurs. So please add proper detail to your question: which value is out of range, where does the error occur, what is the current value of the index, what is the count of whatever the index is referring to?
Gbenbam 5-Aug-23 7:47am    
I did use a debugger. The debugger revealed that at some point in the recursion, iRemaining had no value even though iMiddle and i
helperLeft had values. I am referring to the statement: size_t iRemaining = iMiddle - iHelperLeft;
Rick York 5-Aug-23 19:40pm    
It would be easier to help you if you provide some code that tests these functions.
Gbenbam 6-Aug-23 19:59pm    
#include<iostream>

int main()
{
vector<int> vList{6,10,8,2,9,1,7,4,3,5};

for(int i = 0; i < vList.size(); i++)
{
std::cout << vList[I] <<' ' ;
}

1 solution

Quote:
I did use a debugger. The debugger revealed that at some point in the recursion, iRemaining had no value even though iMiddle and i
helperLeft had values. I am referring to the statement: size_t iRemaining = iMiddle - iHelperLeft;
So now you know why the index is out of range.

Now you use the debugger to backtrack and find out how the values got that way - then you can fix it.

As Richard said, you won't find a solution by googling: nobody else has your specific code!
The debugger doesn;'t look at your code and go "it's that bit!" - it gives you information on what is happening so you can work out when you app goes wrong and then look at why. Debugging is a skill, and the only way to get good at it is to use it - so start working with the debugger and see what info you can find.
 
Share this answer
 
Comments
Gbenbam 5-Aug-23 8:36am    
@OriginalGriff, how does one backtrack with vs 2020?
OriginalGriff 5-Aug-23 9:50am    
In this case it means "run your app again through the debugger looking for when they change to invalid numbers"
You can set conditional breakpoints[^] but that gets advanced pretty quickly.
If you aren't familiar with the debugger, just start by using the basics and move to more complicated stuff when you are familiar with the debugger and proficient at debugging in general.

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