定義:氣泡排序是一種最基礎的交換排序,透過依次比較相鄰的兩個數,將相對小的數放在前面,每一個數都可以像小氣泡一樣,根據自身大小,一點一點向著陣列的一側移動,這種移動被形象的稱為氣泡排序。演算法:假設N個數字要排序完成,總共進行N-1趟排序,每i趟的排序次數為(N-i)次,所以可以用雙重迴圈語句,外層控制迴圈多少趟,內層控制每一趟的迴圈次數常見普通原始碼如下:
lis = [48,3,5, 15, 2, 6,46,27, 14, 38, 44, 15, 36, 50,47]def sortport(): for i in range(len(lis) - 1): for j in range(len(lis) - 1 - i): if lis[j] > lis[j + 1]: lis[j], lis[j + 1] = lis[j + 1], lis[j]# 為了理解排序過程,將內層迴圈結果打印出來 print(lis) return lisif __name__ == '__main__': sortport() print(lis)
列印輸出結果如下:[3, 48, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 48, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 48, 2, 6, 46, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 48, 6, 46, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 48, 46, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 48, 27, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 48, 14, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 48, 38, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 48, 44, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 48, 15, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 48, 36, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 48, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 48, 50, 47]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 15, 2, 6, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 15, 6, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 46, 27, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 46, 14, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 46, 38, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 46, 44, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 46, 15, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 15, 46, 36, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 15, 36, 46, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 15, 36, 46, 48, 47, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 5, 2, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 27, 14, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 44, 15, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 15, 44, 36, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[3, 2, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 15, 14, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 38, 15, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 38, 36, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 27, 15, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
[2, 3, 5, 6, 14, 15, 15, 27, 36, 38, 44, 46, 47, 48, 50]
小小的天流過的淚和汗
總有一天我有屬於我的天
我要一步一步往上爬