python - 我的代码测试没有通过我的冒泡排序算法
问题描述
问题: 冒泡排序是一种算法,它对长度为 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
我写的代码似乎工作正常。但是,编码测试的评分拒绝了我。
我无法理解原因,因为没有列出原因。我的代码有问题吗?
解决方案
换行试试
numbers = input()
至
numbers = raw_input()
可能是测试输入可能采用以下形式
1 5 6 4 3
代替
'1 5 6 4 3'
运行时可能会导致错误input()
推荐阅读
- cmake - CMake / Ninja:当内容未知时递归“清理”输出目录......?
- r - R markdown ...块大小来控制knitr中的图形大小 - 文本大小问题
- azure - 如何在我的 Web 应用程序中使用 Azure AD 连接到 Azure SQL?
- arrays - copyMutableArray 和 copyMutableArray 之间的细微差别#
- python - 当我只想要 Y 和 N 时,如何错误捕获其他字母?
- android - Android WorkManager 10 分钟线程超时来自某处
- python - 是否可以使用变量作为模型名称来查询数据库?
- python - Python中的“RuntimeError:获取对象的str时超出了最大递归深度”
- wordpress - 第一次使用 Bootstrap4 + Wordpress 组合
- java - 在 Jersey Logging 中排除特定端点