2023-10-06 13:31:21 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 11.2 选择排序
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
「选择排序 selection sort」的工作原理非常简单:开启一个循环,每轮从未排序区间选择最小的元素,将其放到已排序区间的末尾。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
设数组的长度为 $n$ ,选择排序的算法流程如图 11-2 所示。
|
|
|
|
|
|
|
|
|
|
1. 初始状态下,所有元素未排序,即未排序(索引)区间为 $[0, n-1]$ 。
|
2023-12-02 06:24:05 +08:00
|
|
|
|
2. 选取区间 $[0, n-1]$ 中的最小元素,将其与索引 $0$ 处的元素交换。完成后,数组前 1 个元素已排序。
|
|
|
|
|
3. 选取区间 $[1, n-1]$ 中的最小元素,将其与索引 $1$ 处的元素交换。完成后,数组前 2 个元素已排序。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
4. 以此类推。经过 $n - 1$ 轮选择与交换后,数组前 $n - 1$ 个元素已排序。
|
|
|
|
|
5. 仅剩的一个元素必定是最大元素,无须排序,因此数组排序完成。
|
|
|
|
|
|
|
|
|
|
=== "<1>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![选择排序步骤](selection_sort.assets/selection_sort_step1.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<2>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step2](selection_sort.assets/selection_sort_step2.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<3>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step3](selection_sort.assets/selection_sort_step3.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<4>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step4](selection_sort.assets/selection_sort_step4.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<5>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step5](selection_sort.assets/selection_sort_step5.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<6>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step6](selection_sort.assets/selection_sort_step6.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<7>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step7](selection_sort.assets/selection_sort_step7.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<8>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step8](selection_sort.assets/selection_sort_step8.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<9>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step9](selection_sort.assets/selection_sort_step9.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<10>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step10](selection_sort.assets/selection_sort_step10.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "<11>"
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![selection_sort_step11](selection_sort.assets/selection_sort_step11.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
<p align="center"> 图 11-2 选择排序步骤 </p>
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
在代码中,我们用 $k$ 来记录未排序区间内的最小元素:
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="selection_sort.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
def selection_sort(nums: list[int]):
|
|
|
|
|
"""选择排序"""
|
|
|
|
|
n = len(nums)
|
|
|
|
|
# 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for i in range(n - 1):
|
|
|
|
|
# 内循环:找到未排序区间内的最小元素
|
|
|
|
|
k = i
|
|
|
|
|
for j in range(i + 1, n):
|
|
|
|
|
if nums[j] < nums[k]:
|
|
|
|
|
k = j # 记录最小元素的索引
|
|
|
|
|
# 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
nums[i], nums[k] = nums[k], nums[i]
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="selection_sort.cpp"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
void selectionSort(vector<int> &nums) {
|
|
|
|
|
int n = nums.size();
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (int i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
int k = i;
|
|
|
|
|
for (int j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k])
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
swap(nums[i], nums[k]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="selection_sort.java"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
void selectionSort(int[] nums) {
|
|
|
|
|
int n = nums.length;
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (int i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
int k = i;
|
|
|
|
|
for (int j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k])
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
int temp = nums[i];
|
|
|
|
|
nums[i] = nums[k];
|
|
|
|
|
nums[k] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="selection_sort.cs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
void SelectionSort(int[] nums) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int n = nums.Length;
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (int i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
int k = i;
|
|
|
|
|
for (int j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k])
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
(nums[k], nums[i]) = (nums[i], nums[k]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="selection_sort.go"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
func selectionSort(nums []int) {
|
|
|
|
|
n := len(nums)
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for i := 0; i < n-1; i++ {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
k := i
|
|
|
|
|
for j := i + 1; j < n; j++ {
|
|
|
|
|
if nums[j] < nums[k] {
|
|
|
|
|
// 记录最小元素的索引
|
|
|
|
|
k = j
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
nums[i], nums[k] = nums[k], nums[i]
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="selection_sort.swift"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
func selectionSort(nums: inout [Int]) {
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for i in nums.indices.dropLast() {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
var k = i
|
|
|
|
|
for j in nums.indices.dropFirst(i + 1) {
|
|
|
|
|
if nums[j] < nums[k] {
|
|
|
|
|
k = j // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
nums.swapAt(i, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="selection_sort.js"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
function selectionSort(nums) {
|
|
|
|
|
let n = nums.length;
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (let i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
let k = i;
|
|
|
|
|
for (let j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k]) {
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
[nums[i], nums[k]] = [nums[k], nums[i]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="selection_sort.ts"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
function selectionSort(nums: number[]): void {
|
|
|
|
|
let n = nums.length;
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (let i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
let k = i;
|
|
|
|
|
for (let j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k]) {
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
[nums[i], nums[k]] = [nums[k], nums[i]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="selection_sort.dart"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
void selectionSort(List<int> nums) {
|
|
|
|
|
int n = nums.length;
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (int i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
int k = i;
|
|
|
|
|
for (int j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k]) k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
int temp = nums[i];
|
|
|
|
|
nums[i] = nums[k];
|
|
|
|
|
nums[k] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="selection_sort.rs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
fn selection_sort(nums: &mut [i32]) {
|
|
|
|
|
let n = nums.len();
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for i in 0..n-1 {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
let mut k = i;
|
|
|
|
|
for j in i+1..n {
|
|
|
|
|
if nums[j] < nums[k] {
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
nums.swap(i, k);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="selection_sort.c"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 选择排序 */
|
|
|
|
|
void selectionSort(int nums[], int n) {
|
|
|
|
|
// 外循环:未排序区间为 [i, n-1]
|
|
|
|
|
for (int i = 0; i < n - 1; i++) {
|
|
|
|
|
// 内循环:找到未排序区间内的最小元素
|
|
|
|
|
int k = i;
|
|
|
|
|
for (int j = i + 1; j < n; j++) {
|
|
|
|
|
if (nums[j] < nums[k])
|
|
|
|
|
k = j; // 记录最小元素的索引
|
|
|
|
|
}
|
|
|
|
|
// 将该最小元素与未排序区间的首个元素交换
|
|
|
|
|
int temp = nums[i];
|
|
|
|
|
nums[i] = nums[k];
|
|
|
|
|
nums[k] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:31:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="selection_sort.zig"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 11.2.1 算法特性
|
|
|
|
|
|
|
|
|
|
- **时间复杂度为 $O(n^2)$、非自适应排序**:外循环共 $n - 1$ 轮,第一轮的未排序区间长度为 $n$ ,最后一轮的未排序区间长度为 $2$ ,即各轮外循环分别包含 $n$、$n - 1$、$\dots$、$3$、$2$ 轮内循环,求和为 $\frac{(n - 1)(n + 2)}{2}$ 。
|
2023-12-28 17:18:37 +08:00
|
|
|
|
- **空间复杂度为 $O(1)$、原地排序**:指针 $i$ 和 $j$ 使用常数大小的额外空间。
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **非稳定排序**:如图 11-3 所示,元素 `nums[i]` 有可能被交换至与其相等的元素的右边,导致两者的相对顺序发生改变。
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![选择排序非稳定示例](selection_sort.assets/selection_sort_instability.png){ class="animation-figure" }
|
2023-10-06 13:31:21 +08:00
|
|
|
|
|
|
|
|
|
<p align="center"> 图 11-3 选择排序非稳定示例 </p>
|