您好,登錄后才能下訂單哦!
#!/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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。