用Python写冒泡排序代码

yipeiwu_com6年前Python基础

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 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]

好了,代码到此就给大家介绍完了,希望对大家有所帮助!

相关文章

python去掉 unicode 字符串前面的u方法

有时我们会碰到类似下面这样的 unicode 字符串: u'\xe4\xbd\xa0\xe5\xa5\xbd' 这明显不是一个正确的 unicode 字符串,可能是在哪个地方转码转...

Python3常见函数range()用法详解

0X01函数说明: python range() 函数可创建一个整数列表,一般用在 for 循环中。 0X02函数语法: range(start,stop[,step]) star...

python通过imaplib模块读取gmail里邮件的方法

本文实例讲述了python通过imaplib模块读取gmail里邮件的方法。分享给大家供大家参考。具体实现方法如下: import imaplib mailserver = imap...

Linux RedHat下安装Python2.7开发环境

Linux RedHat下安装Python2.7开发环境

Linux RedHat下安装Python2.7、pip、ipython环境、eclipse和PyDev环境 准备工作,源Python2.6备份: 根据which python具体目录而...

详解Python里使用正则表达式的ASCII模式

ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码。计算机世界里一开始只有英文,而单字节可...