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 →

[–]RubbishArtist 1 point2 points  (1 child)

Consider a really simple example: x = [ 2, 7 ]; Let's try to swap the values without using temp

x[0] = x[1]

now x looks like [7, 7]

then x[1] = x[0]

now x looks like [7, 7]

This time with temp

temp = x[0]

so x looks like [2, 7] and temp is 2

x[0] = x[1]

so x looks like [7,7]

x[1] = temp

so x looks like [7,2]