Ok, so I completely understand the recursive portion of the Merge Sort algorithm but what I am having problems with is this example of the actual merge portion of the algorithm. In the first conditional statement were one is comparing the first element of the two input vectors I understand, but its the next line were p1 or p2 are being incremented up 1, while specifying which element is to be moved on to the empty vector, I am not understanding. Is it adding element 0 and then ++ to element 1 for the next comparison or is it passing element 1?
I guess my main question is, does the value of a variable get read first and then incremented or is the variables' value incremented and then read?
int newVariable = 0, oldVariableX = 0;
newVariable = oldVariableX++
Does ( newVariable = 0 or newVarivable = 1 ) ?
void Merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2)
{
int n1 = v1.size();
int n2 = v2.size();
int p1 = 0;
int p2 = 0;
while (p1 < n1 && p2 < n2) {
if (v1[p1] < v2[p2]) {
vec.add(v1[p1++]);
} else {
vec.add(v2[p2++]);
}
}
while (p1 < n1) vec.add(v1[p1++]);
while (p2 < n2) vec.add(v2[p2++]);
}