欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python实现冒泡排序

程序员文章站 2022-03-24 16:46:24
...

代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 def bubbleSort(numbers):
    for j in xrange(len(numbers),-1,-1):
        for i in xrange(0,j-1,1):
            if numbers[i] > numbers[i+1]:
                numbers[i],numbers[i+1] = numbers[i+1],numbers[i]
        print numbers

def main():
    numbers = [23,12,9,15,6]
    bubbleSort(numbers)

if __name__ == '__main__':
    main()


输出结果为

[12, 9, 15, 6, 23]
[9, 12, 6, 15, 23]
[9, 6, 12, 15, 23]
[6, 9, 12, 15, 23]
[6, 9, 12, 15, 23]
[6, 9, 12, 15, 23]