Jump to content

  • Log In with Google      Sign In   
  • Create Account


Qestion about passing incremented values


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 06 December 2013 - 08:49 AM

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++]);
}

J-GREEN

Greenpanoply

Sponsor:

#2 exOfde   Members   -  Reputation: 809

Like
4Likes
Like

Posted 06 December 2013 - 08:55 AM

In your example the variable gets first read and then incremented.
Do you wish first do increment or decrement you have to write the operater in front of the variable instead behind like you did.

#3 Lactose!   GDNet+   -  Reputation: 3311

Like
4Likes
Like

Posted 06 December 2013 - 08:56 AM

Where the "++" is in relation to the variable determines what happens

int x1 = 0;
int y = x1++; //x1 = 1, y = 0
int x2 = 0;
int z = ++x2; //x2 = 1, z = 1


#4 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 06 December 2013 - 09:13 AM

Thanks guys :)  that was throwing me all off.


J-GREEN

Greenpanoply

#5 dejaime   Crossbones+   -  Reputation: 4014

Like
0Likes
Like

Posted 06 December 2013 - 09:16 AM

Prefixed increases are more elegant on a computational cost point of view.

 

When you put the increment before the variable, the machine will actually increment and then use that already incremented variable.

In contrast, if you use the postfixed increment, the machine will "create a copy" the value of the variable, increment the variable and then use the (not incremented) copied value; a more costly operation.

 

Please, someone correct me if I'm wrong here.

 

Answering your question, newVariable would equal to 0.

If you used ++oldVariable instead, it would equal to 1.


Edited by dejaime, 06 December 2013 - 09:20 AM.


#6 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 06 December 2013 - 09:31 AM

Actually with that said and now understanding the incrementing better, Why are there two more while loops placed at the end of this function? I mean should not the first while loop be filling the empty vector with sorted merged values from the input vectors?

 

Why are they adding these last two while loops?


J-GREEN

Greenpanoply

#7 Lactose!   GDNet+   -  Reputation: 3311

Like
0Likes
Like

Posted 06 December 2013 - 09:37 AM

Edit: Rephrased this... several times, actually.

 

The first while loop checks if both input vectors have unmerged entries. If they both have unmerged entries, it compares which is the smaller of the 2, and merges that into the output vector.

 

After doing this, it merges the remaining vectors, by just merging whatever is left in vector1, then vector2.

Either vector1 or vector2 will be empty at this point (due to the first while loop having been completed), so the final part really just merges the remaining vector in at the end.


Edited by CoreLactose, 06 December 2013 - 09:49 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS