您好,登录后才能下订单哦!
#!/usr/bin/env python # -*- coding:utf-8 -*- def mp_sort(numbers): for i in range(len(numbers)-1): for j in range(len(numbers) - i -1): if numbers[j] > numbers[j+1]: numbers[j],numbers[j+1] = numbers[j+1],numbers[j] return numbers print mp_sort([2,34,55,43,2,3,56,67,45])
#常见的冒泡排序法,如[7,2,5,1,4]
当 i = 0 ,进入第二个for , 循环次数5-0-1=4次,当j = 0 [2,7,5,1,4]
j = 1 [2,5,7,1,4]
j = 2 [2,5,1,7,4]
j = 3 [2,5,1,4,7]
当 i = 1 ,进入第二个for , 循环次数5-1-1=3次, 当j = 0 [2,5,1,4,7]
j = 1 [2,1,5,4,7]
j = 2 [2,1,4,5,7]
当 i = 2 ,进入第二个for , 循环次数5-2-1=2次, 当j = 0 [1,2,4,5,7]
j = 1 [1,2,4,5,7]
当 i = 3 ,进入第二个for , 循环次数5-3-1=1次, 当j = 0 [1,2,4,5,7]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。