Confusing reference values of variable values during Python sorting...

Asked 2 years ago, Updated 2 years ago, 21 views

I coded sorting What I'm curious about is

This part. Temp refers to alist[i] and alist[i] refers to alist[list] and finally, The alist[last] refers to the temp, so the three values are thought to be the same, but the result is The temp=alist[i] element is stored and does not change... Why is that?

python

2022-09-22 19:02

1 Answers

I think you've implemented a swap. I think you can think of it easily.

temp alist[i] alist[least]
------------------------------------
alist[i] alist[i]     alist[least]     <- temp = alist[i]
alist[i] alist[least] alist[least]     <- alist[i] = alist[least]
alist[i] alist[least] alist[i]         <- alist[least] = temp

So alist[i] and alist[list] are going to change.


2022-09-22 19:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.