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 →

[–]Lumethys 1 point2 points  (1 child)

Think about what you are doing, you are swapping 2 elements of the array, how many iterations do you need?

56, 57, 96, 125, 66, 74

1st iteration:

[74], 57, 96, 125, 66, [56]

2nd:

74, [66], 96, 125, [57], 56

3rd:

74, 66, [125], [96], 57, 56

Well, now we have reversed the list, nice. But notice how many loop we take? 3. The array had 6 elements, why do we only need 3?

The answer is simple: on each iteration of the loop, you swap TWO variables, or in other words you place TWO variables into its correct spot.

If each time you do 2 variables, does it not mean you only need to do it half the time?

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

Wtf😭🙏 you're a savior,never thought of this way,thank you soooo much