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

标签 python sorting bubble-sort

我正在为作业编写一个 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, 0, 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, 0, 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

原始列表已排序

最佳答案

您可以创建原始列表的深拷贝,作为在对数字列表执行排序算法后打印的引用。下面的代码有效。

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

关于python - Python冒泡排序后重新打印原始列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55926547/

相关文章:

python - 在新的 DataFrame 上使用经过训练的分类器

python - 在链表 Python 中对子列表进行排序

java - Elasticsearch 嵌套排序

algorithm - 使用 Bubble Up 的冒泡排序

Python GAE 上传到 Google Docs

python - 绘制按小时和星期几分组的时间序列

php - 在 PHP 中对多维数组进行排序?

c++ - 按 Alpha 顺序对 2D 字符数组进行排序?

c# - C# 中泛型列表的冒泡排序

python - 如何使用 elasticsearch DSL for python 访问响应对象