在Python 3中进行冒泡排序 [英] Bubble Sort in Python 3

查看:102
本文介绍了在Python 3中进行冒泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中编写一个冒泡排序程序。冒泡排序是一种将值列表按顺序排序的算法。

Write a bubble sort program in Python 3. A bubble sort is an algorithm that will sort a list of values into order.

我正在尝试获得此结果

Original List: 4, 9, 74, 0, 9, 8, 28, 1
Sorted List: 0, 1, 4, 8, 9, 9, 28, 74
Number of Passes: 6

我怎么做到?

import sys
def bubblesort(mylist):
        changes = passes = 0
        last = len(mylist)
        swapped = True
        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
                if swapped:
                        passes += 1
                        print('Pass', passes, ':' , ','.join(map(str, mylist)))

        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)

我得到的输出:

Original List:  4,9,74,0,9,8,28,1
Pass 0 : 4,9,74,0,9,8,28,1
Pass 1 : 4,9,0,9,8,28,1,74
Pass 2 : 4,0,9,8,9,1,28,74
Pass 3 : 0,4,8,9,1,9,28,74
Pass 4 : 0,4,8,1,9,9,28,74
Pass 5 : 0,4,1,8,9,9,28,74
Pass 6 : 0,1,4,8,9,9,28,74


Original List: 0, 1, 4, 8, 9, 9, 28, 74
Sorted List: 0, 1, 4, 8, 9, 9, 28, 74
Number of Passes: 6

结果我想要的

Original List: 4, 9, 74, 0, 9, 8, 28, 1
Pass 1:  4, 9, 0, 9, 8, 28, 1, 74
Pass 2:  4, 0, 9, 8, 9, 1, 28, 74
Pass 3 : 0, 4, 8, 9, 1, 9, 28, 74
Pass 4 : 0, 4, 8, 1, 9, 9, 28, 74
Pass 5 : 0, 4, 1, 8, 9, 9, 28, 74
Pass 6 : 0, 1, 4, 8, 9, 9, 28, 74

Original List: 4, 9, 74, 0, 9, 8, 28, 1
Sorted List: 0, 1, 4, 8, 9, 9, 28, 74
Number of Passes: 6


推荐答案

每个当您浏览列表时,您会假定列表已排序。 (1)

Each time you go through the list, you assume the list is sorted. (1)

然后,如果对的顺序不正确,则对列表进行迭代检查每个对。交换他们。 (2)

Then you iterate the list checking every pair, if the pair is not in the correct order. Swap them. (2)

由于该对不正确:整个列表仍不能排序,对吗? (再次假设)(3)

Because of that incorrect pair: "The whole list must still not be sorted, right?" (assuming again) (3)

因此,您必须重复执行直到直到满足条件的某个点为止。那就必须意味着它们都处于正确的顺序。您停止(并打印)。 (4)

Therefore you must repeat until at some point the if-condition is not hit. Then that must mean they are all in correct order. You stop (and print). (4)

def bubble(original_list):
    l = original_list.copy() # make a temporary list
    sorted = False  # Assume list is not sorted at first to kick-start the while loop
    count = 0
    while not sorted: 
        sorted = True # (1) Assume that it's sorted
        for i in range(0, len(l) - 1): # (2) len(l)-1 because the last element                                          
                                       # has no thing on the right to compare to.
            if l[i] > l[i + 1]: # (2) check condition
                sorted = False  # (3) 
                count += 1
                l[i], l[i + 1] = l[i + 1], l[i] # (2) swap

    print("Original: {}".format(original_list)) # (4)
    print("Sorted: {}".format(l))
    print("Number of swaps: {}".format(count))

结论:一些编程人员喜欢假设。

Conclusion: Some programming people love assumptions.

这篇关于在Python 3中进行冒泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆