Python: Print the output N-1 number of times showing the swapswhile doing a selection sort on a random list of numbers

huangapple go评论49阅读模式
英文:

Python: Print the output N-1 number of times showing the swapswhile doing a selection sort on a random list of numbers

问题

我尝试编写一个名为modified_selection_sort的函数它以一个整数值列表作为参数并将其按升序排序该函数应该首先将最大的元素交换到最后一个位置然后将第二大的元素交换到倒数第二个位置依此类推
程序应该使用嵌套循环并在外部循环的每次迭代后输出列表从而输出列表N-1其中N是列表的大小),并且该函数不应返回任何值

到目前为止我编写的代码确实对列表进行了排序但无法以应有的方式显示输出

以下是我尝试过的
```python
def modified_selection_sort(A:list):
    for i in range(len(A)):
        min_idx = i
        max_idx=i
        for j in range(i+1, len(A)):
            if A[min_idx] > A[j]:
                min_idx = j
            if A[max_idx] < A[j]:
                max_idx = j               
            print(A)    #我尝试在这里打印结果?
        A[max_idx], A[min_idx] = A[min_idx], A[max_idx] #这里是交换部分
             
numbers = [int(number) for number in input().split()]
modified_selection_sort(numbers)

以下是我期望的结果:
假设输入列表是:[30 40 20 10]

[30, 10, 20, 40]
[20, 10, 30, 40]
[10, 20, 30, 40]

但我得到的结果是:

[30, 40, 20, 10]
[30, 40, 20, 10]
[30, 40, 20, 10]
[30, 10, 20, 40]
[30, 10, 20, 40]
[30, 40, 20, 10]

我是否漏掉了某个步骤?我是否应该在j循环内部再添加一个循环,以便输出(n-1)次结果?


<details>
<summary>英文:</summary>

I was trying to write a function called modified_selection_sort that takes a list of int values as a parameter and sorts it increasing order. The function should begin by swapping the largest element into the 
last position first, then the second largest into the second-to-last position, etc. 
The program should use nested loops and output the list after each iteration of the outer loop, 
thus outputting the list N-1 times (where N is the size of the list), and the function should have no return value.

The code that I have written so far does sort the list but I can&#39;t get the output to be displayed in the way it should be.

Here&#39;s what I tried:

def modified_selection_sort(A:list):
for i in range(len(A)):
min_idx = i
max_idx=i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
if A[max_idx] < A[j]:
max_idx = j
print(A) #I tried to print the results here?
A[max_idx], A[min_idx] = A[min_idx], A[max_idx] #here's the swapping

numbers = [int(number) for number in input().split()]
modified_selection_sort(numbers)

Here&#39;s what I expected:
say the input list was: [30 40 20 10]

[30, 10, 20, 40]
[20, 10, 30, 40]
[10, 20, 30, 40]

But what I get is:

[30, 40, 20, 10]
[30, 40, 20, 10]
[30, 40, 20, 10]
[30, 10, 20, 40]
[30, 10, 20, 40]
[30, 40, 20, 10]


Am I missing a step somewhere? Should I try making another loop inside the j loop that prints out the output (n-1) number of times?


</details>


# 答案1
**得分**: 2

不需要 `min_idx`。相反,应该从列表的最后一个索引开始,然后反向工作,将未排序部分的项目与`max_idx`处的项目进行比较:

```python
def modified_selection_sort(A):
    for i in range(len(A) - 1, 0, -1):
        max_idx = i
        for j in range(i):
            if A[j] > A[max_idx]:
                max_idx = j
        A[i], A[max_idx] = A[max_idx], A[i]
        print(A)

这样:

modified_selection_sort([30, 40, 20, 10])

输出:

[30, 10, 20, 40]
[20, 10, 30, 40]
[10, 20, 30, 40]

演示:https://replit.com/@blhsing/RequiredCompatibleAnalysts

英文:

You don't need min_idx. You should start from the last index of the list instead and work backwards to keep comparing items in the unsorted portion of the list with the item at max_idx:

def modified_selection_sort(A):
    for i in range(len(A) - 1, 0, -1):
        max_idx = i
        for j in range(i):
            if A[j] &gt; A[max_idx]:
                max_idx = j
        A[i], A[max_idx] = A[max_idx], A[i]
        print(A)

so that:

modified_selection_sort([30, 40, 20, 10])

outputs:

[30, 10, 20, 40]
[20, 10, 30, 40]
[10, 20, 30, 40]

Demo: https://replit.com/@blhsing/RequiredCompatibleAnalysts

答案2

得分: 0

如果您只想在交换后查看列表,请将打印语句直接放在发生交换的那一行下面。

A[max_idx], A[min_idx] = A[min_idx], A[max_idx] # 这里是交换
print(A, min_idx, max_idx)

除此之外,这段代码的逻辑存在问题,它允许在没有益处的情况下交换。打印语句表明程序一直在找到 40 和 10 作为最大值和最小值,然后进行交换,接着再次找到 40 和 10 作为最大值和最小值,然后再次交换,然后以 10 和 20 重复这个过程。

英文:

If you only want to see the list after the swaps, then place the print directly below the line where the swapping happens.

A[max_idx], A[min_idx] = A[min_idx], A[max_idx] #here&#39;s the swapping
print(A, min_idx, max_idx)

Besides that, there is a problem with this code's logic, you are allowing swaps to happen always, even when they aren't beneficial. The prints are indicating that the program is finding 40 and 10 as the max and min, then swapping them, then its finding the 40 and 10 as the max and min again, and swapping again, then repeating the process with 10 and 20

huangapple
  • 本文由 发表于 2023年2月24日 12:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定