冒泡在Python 3.2中排序

时间:2013-10-28 12:14:42

标签: python

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

#!/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 个答案:

答案 0 :(得分:2)

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

  • y=y+1需要在if语句之外,否则只要有两个元素的顺序正确,就会遇到无限循环
  • sorted=True应位于外部循环中,否则如果列表中的最后两个元素按排序顺序排序,则排序将停止

还有一些其他方面可以改进:

  • 在Python中,没有必要在;语句之后加上
  • 您可以执行a, b = b, a之类的转换,而无需使用临时变量
  • 不要使用global个变量,最好将它们作为参数传递
  • 而不是while x < y: ... x=x+1更好地使用for x in range(y)
  • 排序顺序通常是从最小到最大,因此您应该使用>代替<
  • list comprehensions是很棒的东西;了解他们!

总而言之,简化版本可能如下所示:

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)

答案 1 :(得分: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;

如果您的IF声明为真,您只能y=y+1

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

答案 2 :(得分:0)

需要进行两项更改,否则看起来不错!

正如tobias所说,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)

答案 3 :(得分:0)

这不是完成bubbleSort的更好方法。

def bubbleSort(L):

        for i in range(len(L)-1,0,-1):
                for j in range(i):
                        if L[j]>L[j+1]:
                                temp=L[j]
                                L[j]=L[j+1]
                                L[j+1]=temp

        print L
    print "List Sorted . END!!"
L=[4,5,6,7,2,3,8,7,9,6,7]
bubbleSort(L)

这种方法对于成千上万的元素来说是好的。如果您正在寻找像1M这样的bing数字,那么这种方法需要时间来证明您的结果。去寻找其他方法也让我知道;)