1

我今天刚开始学习python。这是我的第一个真正的编程语言......我试图做一个冒泡排序,但它总是失败。我不知道为什么...

#!/usr/bin/python3.2
import random;

i = 0
x = 100
test = []

def createRandom():
        global i
        global test
        while i <= x:
                test.extend([random.randrange(1,100)]);
                i=i+1;
        return test;

def bubblesort():
        sorted=False;
        while sorted!=True:
                y = 0;
                l = len(test)-1
                print(l);
                while y < l:
                        sorted=True;
                        if test[y]<test[y+1]:
                                tmp = test[y]
                                test[y]=test[y+1];
                                test[y+1]=tmp;
                                y=y+1;
                                sorted=False;


createRandom();
bubblesort();
print(test);

错误:

    root@Debian:~/python3# ./bubblesort.py
100
^CTraceback (most recent call last):
  File "./bubblesort.py", line 34, in <module>
    bubblesort();
  File "./bubblesort.py", line 25, in bubblesort
    if test[y]<test[y+1]:
KeyboardInterrupt

谢谢你的帮助!

4

4 回答 4

2

正如评论中已经指出的那样,有两个问题:

  • y=y+1必须在if语句之外,否则只要有两个元素的顺序正确,就会陷入无限循环
  • sorted=True应该在外循环中,否则如果列表中的最后两个元素按排序顺序,您的排序将停止

还有一些可以改进的地方:

  • 在 Python 中,不需要添加;after 语句
  • 您可以进行交换a, b = b, a,而无需使用临时变量
  • 不要global过多使用变量,最好将它们作为参数传递
  • 而不是while x < y: ... x=x+1更好地使用for x in range(y)
  • 排序顺序通常是从小到大,所以你应该使用>而不是<
  • 列表推导是很棒的东西;阅读他们!

将它们放在一起,简化版本可能如下所示:

import random

def createRandom():
    return [random.randrange(1,100) for i in range(100)]

def bubblesort(test):
    is_sorted = False
    while not is_sorted:
        is_sorted= True
        for y in range(len(test) - 1):
            if test[y] > test[y+1]:
                test[y], test[y+1] = test[y+1], test[y]
                is_sorted= False

lst = createRandom()
bubblesort(lst)
print(lst)
于 2013-10-28T12:38:45.717 回答
0
 while y < l:
    sorted=True;
    if test[y]<test[y+1]:
        tmp = test[y]
        test[y]=test[y+1];
        test[y+1]=tmp;
        y=y+1;
        sorted=False;

y=y+1只有当您的 IF 语句为真时,您才会这样做!

尝试将y=y+1if 放在外面,它应该可以工作。

于 2013-10-28T12:24:34.923 回答
0

有两个需要做的改变,否则看起来不错!

正如托比亚斯所说, moev 内部 while 循环之外的 sorted=True 。您需要将 y = y + 1 移到 if 语句之外。

#!/usr/bin/python3.2
import random;

i = 0
x = 100
test = []

def createRandom():
        global i
        global test
        while i <= x:
                test.extend([random.randrange(1,100)]);
                i=i+1;
        return test;

def bubblesort():
        sorted=False;
        while sorted!=True:
                y = 0;
                l = len(test)-1
                print(l);
                sorted=True;
                while y < l:
                        if test[y]<test[y+1]:
                                tmp = test[y]
                                test[y]=test[y+1];
                                test[y+1]=tmp;
                                sorted=False;
                        y=y+1;


createRandom();
bubblesort();
print(t)
于 2013-10-28T12:24:52.143 回答
0

这不是完成冒泡排序的更好方法吗?

def气泡排序(L):

        对于范围内的 i (len(L)-1,0,-1):
                对于范围(i)中的j:
                        如果 L[j]>L[j+1]:
                                温度=L[j]
                                L[j]=L[j+1]
                                L[j+1]=温度

        打印 L
    打印“列表排序。结束!!”
L=[4,5,6,7,2,3,8,7,9,6,7]
冒泡排序(L)

这种方法适用于数千个元素。如果您正在寻找像 1M 这样的 bing 数字,那么这种方法将需要时间来证明您的结果。寻求其他方法,也让我知道;)

于 2016-04-07T11:26:16.113 回答