選擇排序是一種簡單直觀的排序算法,無論什么數據進去都是O(n2)
的時間復雜度。所以用到它的時候,數據規模越小越好。唯一的好處可能就是不占用額外的內存空間了吧。
算法過程描述
- 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。
- 再從剩余未排序元素中繼續尋找最小(大)元素,然后放到已排序序列的末尾。
- 重復第二步,直到所有元素均排序完畢。
動圖演示
selectionSort.gif
復雜度
假設序列有n
個元素,n>1
,根據算法步驟,第1輪
需在n
個元素中遍歷n
次查找到最小
的元素,第2輪
需在剩余的(n-1)
個元素中遍歷(n-1)
次找到最小
的元素,… 第n-1輪
需在剩余的2
個元素中找到最小
的元素,第n輪
剩余1
個元素放在已排序元素的末尾
。
函數表達式為:
f(n) = n+(n-1)+…+2+1
f(n) = (n+1)*n/2
f(n) = (n2+n)/2
用大O
表示法,忽略常量、低階和常數系數。
時間復雜度為:O(n2)
空間復雜度為:并未開辟額外空間, 所以為O(1)
穩定性: 不穩定
代碼實現(Swift)
假設要對以下數組進行選擇排序:
let numbers = [1, 4, 3, 2, 0, 5, 9, 7, 8, 6]
對n
個元素進行選擇排序,主要是確定每一輪的最小(大)值的角標, 來和該輪的第一個元素進行位置交換, 如果相同, 則不需要交換:
func selectionSort(numbers:[Int]) -> [Int] {
var sortedNumbers = numbers
for i in 0..<sortedNumbers.count - 1 {
print("\(sortedNumbers) (\(i)th circle begin)");
var minIndex = i
print("minIndex init with \(i)")
for j in i + 1..<sortedNumbers.count {
if sortedNumbers[minIndex] > sortedNumbers[j] {
minIndex = j
print("minIndex changed to \(minIndex)")
}
}
if minIndex != i {
sortedNumbers.swapAt(minIndex, i)
print("swap at \(minIndex) and \(i)")
}
}
return sortedNumbers
}
let sortedNumbers = selectionSort(numbers: numbers)
print("\n\(sortedNumbers) (selection sort result)")
終端打印結果:
[1, 4, 3, 2, 0, 5, 9, 7, 8, 6] (0th circle begin)
minIndex init with 0
minIndex changed to 4
swap at 4 and 0
[0, 4, 3, 2, 1, 5, 9, 7, 8, 6] (1th circle begin)
minIndex init with 1
minIndex changed to 2
minIndex changed to 3
minIndex changed to 4
swap at 4 and 1
[0, 1, 3, 2, 4, 5, 9, 7, 8, 6] (2th circle begin)
minIndex init with 2
minIndex changed to 3
swap at 3 and 2
[0, 1, 2, 3, 4, 5, 9, 7, 8, 6] (3th circle begin)
minIndex init with 3
[0, 1, 2, 3, 4, 5, 9, 7, 8, 6] (4th circle begin)
minIndex init with 4
[0, 1, 2, 3, 4, 5, 9, 7, 8, 6] (5th circle begin)
minIndex init with 5
[0, 1, 2, 3, 4, 5, 9, 7, 8, 6] (6th circle begin)
minIndex init with 6
minIndex changed to 7
minIndex changed to 9
swap at 9 and 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (7th circle begin)
minIndex init with 7
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (8th circle begin)
minIndex init with 8
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (selection sort result)
當測試數據修改為:
let numbers = [1, 4, 3, 2, 0, 5, 6, 7, 8, 9]
終端打印結果:
[1, 4, 3, 2, 0, 5, 6, 7, 8, 9] (0th circle begin)
minIndex init with 0
minIndex changed to 4
swap at 4 and 0
[0, 4, 3, 2, 1, 5, 6, 7, 8, 9] (1th circle begin)
minIndex init with 1
minIndex changed to 2
minIndex changed to 3
minIndex changed to 4
swap at 4 and 1
[0, 1, 3, 2, 4, 5, 6, 7, 8, 9] (2th circle begin)
minIndex init with 2
minIndex changed to 3
swap at 3 and 2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (3th circle begin)
minIndex init with 3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (4th circle begin)
minIndex init with 4
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (5th circle begin)
minIndex init with 5
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (6th circle begin)
minIndex init with 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (7th circle begin)
minIndex init with 7
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (8th circle begin)
minIndex init with 8
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (selection sort result)
可以看出, minIndex 為3
之后已經成為了有序數組
, 但是還是繼續了排序的操作;
細想一下,每次掃描未排序區間只選取一個最小值,那么是否可以每次掃描時選擇一個最小元素和一個最大元素,分別放置在有序區間的尾部和尾部有序區間的頭部呢?
代碼優化
在循環選取時,每次掃描未排序區間
分別選擇最小、最大
值放于數組始、末
位置, 當最小元素的位置+1 =
最大元素的位置時,數據已經全部有序,可以提前退出:
func selectionSort(numbers:[Int]) -> [Int] {
var sortedNumbers = numbers
for i in 0..<sortedNumbers.count - 1 {
print("\(sortedNumbers) (\(i)th circle begin)");
var minIndex = i
print("minIndex init with \(i)")
// 最大下標也要隨著最小下標的增加而對應減小
var maxIndex = sortedNumbers.count - i - 1
print("maxIndex init with \(maxIndex)")
// 內部循環比較, 也是在雙向縮小范圍
for j in i + 1..<sortedNumbers.count - i {
if sortedNumbers[minIndex] > sortedNumbers[j] {
minIndex = j
print("minIndex changed to \(minIndex)")
}
if sortedNumbers[maxIndex] < sortedNumbers[j] {
maxIndex = j
print("maxIndex changed to \(maxIndex)")
}
}
if minIndex != i {
sortedNumbers.swapAt(minIndex, i)
print("swap at \(minIndex) and \(i)")
}
if maxIndex != sortedNumbers.count - i - 1 {
sortedNumbers.swapAt(maxIndex, sortedNumbers.count - i - 1)
print("swap at \(maxIndex) and \(sortedNumbers.count - i - 1)")
}
// 一定要交換完成 才判斷是否符合提前退出的條件
if (minIndex + 1 == maxIndex) {
break
}
}
return sortedNumbers
}
let sortedNumbers = selectionSort(numbers: numbers)
print("\n\(sortedNumbers) (selection sort result)")
終端打印結果:
[1, 4, 3, 2, 0, 5, 6, 7, 8, 9] (0th circle begin)
minIndex init with 0
maxIndex init with 9
minIndex changed to 4
swap at 4 and 0
[0, 4, 3, 2, 1, 5, 6, 7, 8, 9] (1th circle begin)
minIndex init with 1
maxIndex init with 8
minIndex changed to 2
minIndex changed to 3
minIndex changed to 4
swap at 4 and 1
[0, 1, 3, 2, 4, 5, 6, 7, 8, 9] (2th circle begin)
minIndex init with 2
maxIndex init with 7
minIndex changed to 3
swap at 3 and 2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (3th circle begin)
minIndex init with 3
maxIndex init with 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (4th circle begin)
minIndex init with 4
maxIndex init with 5
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (selection sort result)
可以看出, 四次循環就完成了。
為什么“選擇排序”是不穩定的?
舉例說明:
假設某學校積分入學剩余
2
個學位,A
、B
、C
三位學生先后報名,積分分別為[A(90), B(90), C(100)]
,積分從高到低排序,前兩名獲得入學資格,如果使用選擇排序:
第一輪排序會將A
和C
交換,變成[C(100), B(90), A(90)]
,此時排序已完成;
A
、B
同積分,但原來A
比B
優先報名的,本應優先取得入學資格,排序后卻變成了B
在A
的前面,現實中必然是不公平的。
因此可以看出,選擇排序是不穩定的。
回目錄:常用的排序算法
結語
路漫漫其修遠兮,吾將上下而求索~
.End