functions in python, questions about swap

Asked 2 years ago, Updated 2 years ago, 45 views

*This is my first time asking a question, so I would appreciate it if you could accept it warmly.

We created two functions min_index, selections as follows:

j=min_index(A[i:])
    A[i], A[i+j] = A[i+j], A[i]

If you do this with respect to the part of , you can do whatever you want, but

A[i], A[i+min_index(A[i:]]] = A[i+min_index(A[i:])], A[i]

Then, I am confused that swap is not performed.

I'm still at a standstill because I don't know much about Python and don't get any error codes.I would appreciate it if someone could let me know.

defmin_index(A):
    index = 0
    m = A [0]
for i in range (1,len(A)):
    if A[i]<m:
        m=A[i]
        index=i
return index

def selectionsort(A,N):
    for i in range (N):
        j=min_index(A[i:])
        A[i], A[i+j] = A[i+j], A[i]
        # A[i], A[i+min_index(A[i:])] = A[i+min_index(A[i:]), A[i]
return A

python sort

2022-09-29 20:26

1 Answers

First of all, the indentation of the code you are currently asking is Python meaningless, so I'm rereading it as follows:

defmin_index(A):
    index = 0
    m = A [0]
    for i in range (1,len(A)):
        if A[i]<m:
            m=A[i]
            index=i
    return index

def selectionsort(A,N):
    for i in range (N):
        j=min_index(A[i:])
        A[i], A[i+j] = A[i+j], A[i]
        # A[i], A[i+min_index(A[i:])] = A[i+min_index(A[i:]), A[i]
    return A

Rather than saying "swap doesn't work", it's better to think, "The result is not as expected because the substitute calculation depends on the detailed order of execution."(As a result, it's not "swap", so it's the same thing.)

Python evaluation order is based on

  • Left→Right
  • For substitution statements, right side of = → left side of =

in this order.

If you are thinking about a swap substitution statement, it will appear in the document as follows:

6.16.Evaluation order

 expr3, expr4 = expr1, expr2

(1, 2, ... are the evaluation order.)

Although the detailed order of substitution processing is not included because it shows the evaluation order of the expression, it is evaluated immediately (or as part of each evaluation of 7.2.Assignment statements on the left.

If you look at the details step by step, it looks like the following.Since it is difficult to understand just by verbal explanation, I will include an example of the state when A=[3,2,1], i=0.


[1] Calculate A[i+min_index(A[i:]]]3 2 1 → 3 2 1 i+min_index(A[i:]) so the result is 1
[2] Calculate A[i]3 2 1 → 3 2 1 Results are 3
[3] Replace the results in [1] above with A[i]3 2 1 → 1 2 1
[4] Replace the above [2] result with A[i+min_index(A[i:]]1 2 1 → 3 2 1 At this point, i+min_index(A[i:]) becomes 0

So it doesn't end up as a swap process.

To prevent this misunderstanding, it's more important to say, "Don't write in such a way that the details of the execution order change the results than to say, "You should fully understand Python's specifications."

In addition, min_index is a time-consuming process proportional to the length of the array (slice), so it is also disadvantageous to repeat such a process over and over again.

You should always be careful to write like the one that is coded in.


2022-09-29 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.