首页 > 解决方案 > 我的代码测试没有通过我的冒泡排序算法

问题描述

问题: 冒泡排序是一种算法,它对长度为 N 的序列进行排序,以检查两个相邻元素以改变它们的位置。冒泡排序可以执行 N 次,如下所示。

将第一个值与第二个值进行比较,如果第一个值更大,则更改位置。比较第二个值和第三个值,如果第二个值更大,它会改变它的位置。... 比较 N - 1 和 N - 的值,如果 N - 1 的值更大,则更改位置。“Supchan”我当然知道冒泡排序的结果。但是,由于N非常大,执行K次以上步骤需要很长时间。编写一个程序,帮助你找到冒泡排序的中间过程。

我的代码

def bubble(list):
    temp = 0
    for i in range(0, len(list)): 
        for j in range(i+1, len(list)): 
            if (list[i] > list[j]):
                temp = list[i]
                list[i] = list[j]
                list[j] = temp
    return list


numbers = input()
items = [int(num) for num in numbers.split()]

print(bubble(items))

测试条件


N and K are given in the first line.

The second line gives the status of the first sequence. That is, N integers forming the first sequence are given in turn, with spaces between them.

1 ≤ N ≤ 100,000
1 ≤ K ≤ N
Each term in the sequence is an integer from 1 to 1,000,000,000.

输入输出

input: 3 1 2 5 4
output: 1 2 3 4 5

我写的代码似乎工作正常。但是,编码测试的评分拒绝了我。

我无法理解原因,因为没有列出原因。我的代码有问题吗?

标签: pythonalgorithm

解决方案


换行试试

numbers = input()

numbers = raw_input()

可能是测试输入可能采用以下形式

1 5 6 4 3

代替

'1 5 6 4 3'

运行时可能会导致错误input()


推荐阅读