python代码实现冒泡排序代码其实很简单,具体代码如下所示:
代码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 numbersdef 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]
好了,代码到此就给大家介绍完了,希望对大家有所帮助!
免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:chuangshanghai#qq.com(把#换成@)