Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python气泡排序,正在考虑10个<;5、7等_Python - Fatal编程技术网

Python气泡排序,正在考虑10个<;5、7等

Python气泡排序,正在考虑10个<;5、7等,python,Python,我创建了一个冒泡排序程序,在该程序中,用户可以输入存储在数组中的值,该数组将通过冒泡排序算法进行排序,但是如果我输入值1、2、3、4、5、6、7、8、9、10、10,在1将排序列表更改为1、10、2、3、4、5、6、7后,输出将不是我所期望的,8,9这让我相信10被解读为1而不是10,但我不知道为什么 def initlist(): unsortedlist=[] for counter in range (0,10): print("Please en

我创建了一个冒泡排序程序,在该程序中,用户可以输入存储在数组中的值,该数组将通过冒泡排序算法进行排序,但是如果我输入值1、2、3、4、5、6、7、8、9、10、10,在1将排序列表更改为1、10、2、3、4、5、6、7后,输出将不是我所期望的,8,9这让我相信10被解读为1而不是10,但我不知道为什么

    def initlist():
    unsortedlist=[]

    for counter in range (0,10):
        print("Please enter value", (counter+1))
        temp = input("")
        unsortedlist.append(temp)
    return unsortedlist


def BubbleSort(unsortedlist):
    passes = 0
    print ("Fixed Loop")
    print ("Initial State:", unsortedlist)

    passes = 0
    swaps = 0
    for outer in range(len(unsortedlist) -1, 0, -1):
        passes += 1
        for inner in range (outer):
            if unsortedlist[inner] > unsortedlist[inner + 1]:
                swaps +=1
                temp = unsortedlist[inner]
                unsortedlist[inner] = unsortedlist[inner+1]
                unsortedlist[inner + 1] = temp

            print("Pass", passes, ":", unsortedlist)
    input("Press Any Key To Continue")
输出

Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 9 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
temp=input(“”)
将输入读取为字符串。你在这里做字典排序

如果要对整数进行排序,请更改为
temp=int(输入(“”)

您似乎正在对字符串进行排序?!这自然会导致
'10'
成为
'2'