Last answered:

03 May 2023

Posted on:

29 Mar 2023

1

Is "j" really necessary?

I was a bit confused by the code "j = i" since I didn't understand the purpose of it. After removing it and replacing j per i in the rest of the code the function works fine, so I wonder why Giles use it.


Thanks.

1 answers ( 0 marked as helpful)
Posted on:

03 May 2023

0

Hi Guillen, 

I had the same though and I woke up this morning thinking about it.

I believe that the reason for the j is that the consequence of not creating assign i to j, j=i, would result in additional computation.


As j continues to decrease in the inner-loop, if there was only i, there are extra calculations as it has to "catch up". This would be the equivalent of having walk down a hallway to get to the end but stepping back as you count down on your fingers for every step.


Having to re-increment i after running the while loop adds computational strain. The end result is the same but on a small data set.


This is what helped me think through it but I would like to see the response to this question to see if I am thinking about this correctly and hopefully get an exmample or a list that can demonstrate the situation..

Submit an answer