泡泡排序python仅排序一次

发布于 2025-01-31 19:21:29 字数 311 浏览 3 评论 0原文

谁能帮助我确定此程序中的错误排序?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2025-02-07 19:21:29

更改外环的范围:

for i in range (0, len(a)):

Change the range in the outer loop:

for i in range (0, len(a)):
烈酒灼喉 2025-02-07 19:21:29

循环中的小问题。

def bsort(a):
    for i in range (0, len(a)-1):
        for j in range(len(a)-2): ## updated here
            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)

输出为[1,2,2,2,5,5,6,7,9]

Minor issues in for loop.

def bsort(a):
    for i in range (0, len(a)-1):
        for j in range(len(a)-2): ## updated here
            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)

Output is [1, 2, 2, 5, 5, 6, 7, 9]

木格 2025-02-07 19:21:29

循环中有一个错误,您似乎交换了错误的索引。

    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]
                    print(f'swapping {a[j]} with {a[j+1]}')
                    print(f'swapping {a[j+1]} with {temp}')
                    a[j] = a[j+1]
                    a[j+1] = temp
    
    num = [1,2,6,5,7,2,5,9]
    
    bsort(num)
    print(num)
    

用5
交换6
用6
交换5
用2交换7
用7交换2
用2交换6
用6
交换2
用5交换7
用7交换5
[1、2、5、2、6、5、7、9]

尝试

    def bubble_sort(arr):
        for itm in arr:
            for idx in range(len(arr)-1):
                if arr[idx] > arr[idx+1]:
                    print(f'swapping {arr[idx]} with {arr[idx+1]}')
                    arr[idx],arr[idx+1]=arr[idx+1],arr[idx]
        return arr
    print(bubble_sort(num))

以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.

    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]
                    print(f'swapping {a[j]} with {a[j+1]}')
                    print(f'swapping {a[j+1]} with {temp}')
                    a[j] = a[j+1]
                    a[j+1] = temp
    
    num = [1,2,6,5,7,2,5,9]
    
    bsort(num)
    print(num)
    

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

    def bubble_sort(arr):
        for itm in arr:
            for idx in range(len(arr)-1):
                if arr[idx] > arr[idx+1]:
                    print(f'swapping {arr[idx]} with {arr[idx+1]}')
                    arr[idx],arr[idx+1]=arr[idx+1],arr[idx]
        return arr
    print(bubble_sort(num))

swapping 5 with 2
swapping 6 with 5
[1, 2, 2, 5, 5, 6, 7, 9]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文