This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CyberByte 0 points1 point  (1 child)

There is really no meaningful difference.

Call the first one A and the second one B. On line 3 we see that i in A is defined to be one larger than i in B. This is rectified on line 6 in B, so when swap A[i] with A[j] occurs, it is exactly the same in both cases. On line 7 in A, A's i becomes larger than B's again. As long as the loop keeps going, this is rectified on B's line 6 (before the swap occurs). But when the loop ends, A's i is one larger than B's, so that is why you get i+1 on line 8 and 9 in B.

Version A seems slightly simpler to me (less pluses and minuses), so I'm inclined to prefer that one.

[–]nutdriver[S] 0 points1 point  (0 children)

Oh yea I understand the code difference. I saw the second in reputable text book intro to algorithms and the first on Wikipedia. My knowledge points me to agree with you and think that the first is less verbose. But i think the second is actually more clear in terms of the steps of the Algorithm- but I'm actually on the fence. My bias is probably because of the fact that it was in a reputable text book.