繁体   English   中英

在Python Bubble排序后重新打印原始列表

[英]Re-print Original List After Python Bubble Sort

我正在编写用于作业分配的Python 3 Bubble Sort程序,但是在对列表进行排序后,我无法弄清楚如何获取要重新打印的原始列表(又称未排序列表)。

以下已发布的问题几乎可以一直得到答案,但没有为第二份印刷的原始清单提供解决方案:

在Python 3中进行冒泡排序

类似,但不能解决打印问题: 冒泡排序作业

我希望重新发布可以得到完整的答案

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,0,9,8,28,1,74

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

第3遍:0、4、8、9、1、9、28、74

通道4:0、4、8、1、1、9、9、28、74

第5遍:0、4、1、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,0,9,8,28,1,74

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

第3遍:0、4、8、9、1、9、28、74

通道4:0、4、8、1、1、9、9、28、74

第5遍:0、4、1、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

原始列表显示已排序

我会另外列出一个内容相同的列表,然后排序。 请注意,如果您只是重新命名,它将指向原始列表并进行修改:

new_list = original_list

会给你带来麻烦。

new_list = original_list[:]

将工作。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM