泡泡排序python仅排序一次
谁能帮助我确定此程序中的错误排序?
def bsort(a):
for i in range (0, len(a)-1):
for j in range(i):
if a[j]> a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
num = [1,2,6,5,7,2,5,9]
bsort(num)
print(num)
它仅对列表进行排序一次。
Can anyone help me in identifying the error in this program for bubble sort?
def bsort(a):
for i in range (0, len(a)-1):
for j in range(i):
if a[j]> a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
num = [1,2,6,5,7,2,5,9]
bsort(num)
print(num)
It sorts the list only once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改外环的范围:
Change the range in the outer loop:
循环中的小问题。
输出为
[1,2,2,2,5,5,6,7,9]
Minor issues in for loop.
Output is
[1, 2, 2, 5, 5, 6, 7, 9]
循环中有一个错误,您似乎交换了错误的索引。
用5
交换6
用6
交换5
用2交换7
用7交换2
用2交换6
用6
交换2
用5交换7
用7交换5
[1、2、5、2、6、5、7、9]
尝试
以2
交换5
用5
交换6
[1,2,2,5,5,6,7,9]
there is a mistake in your loop, you seem to swap the wrong indexes.
swapping 6 with 5
swapping 5 with 6
swapping 7 with 2
swapping 2 with 7
swapping 6 with 2
swapping 2 with 6
swapping 7 with 5
swapping 5 with 7
[1, 2, 5, 2, 6, 5, 7, 9]
Try this instead
swapping 5 with 2
swapping 6 with 5
[1, 2, 2, 5, 5, 6, 7, 9]