Python冒泡排序后重新打印原始列表

问题描述 投票:1回答:2

我正在编写一个Python 3冒泡排序程序用于家庭作业,我无法弄清楚如何在列表已经排序之后将原始列表重新打印(也就是未排序的列表)。

以下已发布的问题几乎一直得到答案,但没有为第二个打印的原始列表提供解决方案:

Bubble Sort in Python 3

类似但不解决印刷问题:Bubble Sort Homework

我希望转发我可以得到一个完整的答案

import sys

def bubblesort(mylist):
    changes = passes = 0
    last = len(mylist)
    swapped = True
    # This original list (below) correctly prints as unsorted:
    print("Original List: ", ','.join(map(str, mylist)) )
    while swapped:
        swapped = False

        for j in range(1, last):
            if mylist[j - 1] > mylist[j]:
                mylist[j], mylist[j - 1] = mylist[j - 1], mylist[j]  # Swap
                changes += 1
                swapped = True
                last = j

        # Only prints and increases number of passes if there was a swap
        # Remove if statement for the correct number of passes
        if(swapped):
          passes += 1
          print('Pass', passes, ':' , ','.join(map(str, mylist)))

    # This original list (below) prints sorted:
    print("\nOriginal List: ", ','.join(map(str, mylist)) )
    print("Sorted List: ", ','.join(map(str, mylist)) )
    print("Number of passes =",passes)
    return mylist

print("Welcome to a Bubble Sort Algorithm in Python!")

mylist = " "
while True:
    print("\nBubble sort in Python 3 Program")
    mylist = input("Enter a the value or type Exit to exit: ")
    if (mylist == "exit" or mylist == "Exit" or mylist == "EXIT"):
        print("Goodbye")
        sys.exit()
    else:
        mylist = [int(v) for v in mylist.split(',')]
        bubblesort(mylist)

该计划应产生以下印刷结果:

原始列表:4,9,74,0,9,8,28,1

通过1:4,9,8,9,8,28,1,74

传球2:4,0,9,8,9,1,28,74

传球3:0,4,8,9,1,9,28,74

通过4:0,4,8,1,9,9,28,74

传球5:0,4,1,8,9,9,28,74

通过6:0,1,4,8,9,9,28,74

原始列表:4,9,74,0,9,8,28,1

排序列表:0,1,4,8,9,9,28,74

通行证数量:6

实际打印结果:

原始列表:4,9,74,0,9,8,28,1

通过1:4,9,8,9,8,28,1,74

传球2:4,0,9,8,9,1,28,74

传球3:0,4,8,9,1,9,28,74

通过4:0,4,8,1,9,9,28,74

传球5:0,4,1,8,9,9,28,74

通过6:0,1,4,8,9,9,28,74

原始列表:0,1,4,8,9,9,28,74

排序列表:0,1,4,8,9,9,28,74

原始列表显示已排序

python sorting bubble-sort
2个回答
0
投票

在对数字列表执行排序算法后,您可以创建原始列表的深层副本作为打印参考。下面的代码工作。

import sys
from copy import deepcopy

def bubblesort(mylist):
    changes = passes = 0
    last = len(mylist)
    swapped = True
    originalList = deepcopy(mylist)
    # This original list (below) correctly prints as unsorted:
    print("Original List: ", ','.join(map(str, mylist)) )
    while swapped:
        swapped = False

        for j in range(1, last):
            if mylist[j - 1] > mylist[j]:
                mylist[j], mylist[j - 1] = mylist[j - 1], mylist[j]  # Swap
                changes += 1
                swapped = True
                last = j

        # Only prints and increases number of passes if there was a swap
        # Remove if statement for the correct number of passes
        if(swapped):
          passes += 1
          print('Pass', passes, ':' , ','.join(map(str, mylist)))

    # This original list (below) prints sorted:
    print("\nOriginal List: ", ','.join(map(str, originalList)) )
    print("Sorted List: ", ','.join(map(str, mylist)) )
    print("Number of passes =",passes)
    return mylist

0
投票

我会做一个具有相同内容的附加列表并对其进行排序。请注意,如果您只是创建一个新名称,它将指向原始列表并将修改:

new_list = original_list

会告诉你问题。

new_list = original_list[:]

将工作。

© www.soinside.com 2019 - 2024. All rights reserved.