Bubble Sort(Sinking Sort)
兩兩比較,把最大的數字往數組的最後排
1. 兩兩比較,把最大的數字往數組的最後排
origin: [30, 12, 45, 15, 3]
first 45: [30, 12, 15, 3, 45]
second 30: [12, 15, 3, 30, 45]
third 15: [12, 3, 15, 30, 45]
forth 12: [3, 12, 15, 30, 45]
fifth 3: [3, 12, 15, 30, 45]
2. 只要有一輪沒有交換,代表已經排序完成
定義noSwap做紀錄
forth 12: [3, 12, 15, 30, 45] => noSwap = true
fifth 3: [3, 12, 15, 30, 45]
|
|
時間複雜度
Best case: log(n)
Worst case: log(n square)
|
|