Question about 6.7 in 1.4.7

#1

Hi,

The solution has condition & A[i] != i + 1, which seems to be already covered by A[A[i] - 1] != A[i]. Because if A[i] == i + 1, then A[A[i] - 1] == A[i + 1 - 1] == A[i], isn’t it?

Thanks!

0 Likes

#2

Hey Ray,

You are right, and the reason I put that before is because we want to prevent unnecessary swaps for two identical indices. However, I think readability trumps efficiency here. Therefore, I just remove that check.

0 Likes

#3

Cool. Thanks for your reply!

0 Likes