用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]
好了,代码到此就给大家介绍完了,希望对大家有所帮助!
相关推荐
hang0 2020-08-16
风吹夏天 2020-07-07
小海 2020-06-25
清溪算法君老号 2020-06-06
wonner 2020-06-03
清溪算法君老号 2020-06-01
RememberMePlease 2020-05-01
清溪算法君老号 2020-04-25
rein0 2020-04-21
rein0 2020-04-18
qingsongzdq 2020-03-03
wonner 2020-02-25
horizonheart 2020-02-23
baike 2020-02-16
# 第三题:使用python实现冒泡排序def BubbleSort: long = len for i in range: for j in range: if list[i] < list[j]:
GhostLWB 2020-01-11
singer 2020-01-08
蜗牛慢爬的李成广 2020-01-04