2023-05-24 00:35:46 +08:00
|
|
|
|
# 选择排序
|
|
|
|
|
|
2023-08-20 23:28:30 +08:00
|
|
|
|
「选择排序 selection sort」的工作原理非常直接:开启一个循环,每轮从未排序区间选择最小的元素,将其放到已排序区间的末尾。
|
2023-05-24 11:45:52 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
设数组的长度为 $n$ ,选择排序的算法流程如下图所示。
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
|
|
|
|
1. 初始状态下,所有元素未排序,即未排序(索引)区间为 $[0, n-1]$ 。
|
|
|
|
|
2. 选取区间 $[0, n-1]$ 中的最小元素,将其与索引 $0$ 处元素交换。完成后,数组前 1 个元素已排序。
|
|
|
|
|
3. 选取区间 $[1, n-1]$ 中的最小元素,将其与索引 $1$ 处元素交换。完成后,数组前 2 个元素已排序。
|
|
|
|
|
4. 以此类推。经过 $n - 1$ 轮选择与交换后,数组前 $n - 1$ 个元素已排序。
|
2023-08-20 14:51:39 +08:00
|
|
|
|
5. 仅剩的一个元素必定是最大元素,无须排序,因此数组排序完成。
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
|
|
|
|
=== "<1>"
|
|
|
|
|
![选择排序步骤](selection_sort.assets/selection_sort_step1.png)
|
|
|
|
|
|
|
|
|
|
=== "<2>"
|
|
|
|
|
![selection_sort_step2](selection_sort.assets/selection_sort_step2.png)
|
|
|
|
|
|
|
|
|
|
=== "<3>"
|
|
|
|
|
![selection_sort_step3](selection_sort.assets/selection_sort_step3.png)
|
|
|
|
|
|
|
|
|
|
=== "<4>"
|
|
|
|
|
![selection_sort_step4](selection_sort.assets/selection_sort_step4.png)
|
|
|
|
|
|
|
|
|
|
=== "<5>"
|
|
|
|
|
![selection_sort_step5](selection_sort.assets/selection_sort_step5.png)
|
|
|
|
|
|
|
|
|
|
=== "<6>"
|
|
|
|
|
![selection_sort_step6](selection_sort.assets/selection_sort_step6.png)
|
|
|
|
|
|
|
|
|
|
=== "<7>"
|
|
|
|
|
![selection_sort_step7](selection_sort.assets/selection_sort_step7.png)
|
|
|
|
|
|
|
|
|
|
=== "<8>"
|
|
|
|
|
![selection_sort_step8](selection_sort.assets/selection_sort_step8.png)
|
|
|
|
|
|
|
|
|
|
=== "<9>"
|
|
|
|
|
![selection_sort_step9](selection_sort.assets/selection_sort_step9.png)
|
|
|
|
|
|
|
|
|
|
=== "<10>"
|
|
|
|
|
![selection_sort_step10](selection_sort.assets/selection_sort_step10.png)
|
|
|
|
|
|
|
|
|
|
=== "<11>"
|
|
|
|
|
![selection_sort_step11](selection_sort.assets/selection_sort_step11.png)
|
|
|
|
|
|
|
|
|
|
在代码中,我们用 $k$ 来记录未排序区间内的最小元素。
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="selection_sort.py"
|
|
|
|
|
[class]{}-[func]{selection_sort}
|
2023-05-24 00:35:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="selection_sort.cpp"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="selection_sort.java"
|
|
|
|
|
[class]{selection_sort}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="selection_sort.cs"
|
|
|
|
|
[class]{selection_sort}-[func]{selectionSort}
|
2023-05-24 00:35:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="selection_sort.go"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="selection_sort.swift"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="selection_sort.js"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="selection_sort.ts"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="selection_sort.dart"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="selection_sort.rs"
|
|
|
|
|
[class]{}-[func]{selection_sort}
|
2023-05-24 00:35:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```c title="selection_sort.c"
|
2023-05-24 00:35:46 +08:00
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="selection_sort.zig"
|
|
|
|
|
[class]{}-[func]{selectionSort}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 算法特性
|
|
|
|
|
|
2023-08-27 23:40:39 +08:00
|
|
|
|
- **时间复杂度为 $O(n^2)$、非自适应排序**:外循环共 $n - 1$ 轮,第一轮的未排序区间长度为 $n$ ,最后一轮的未排序区间长度为 $2$ ,即各轮外循环分别包含 $n$、$n - 1$、$\dots$、$3$、$2$ 轮内循环,求和为 $\frac{(n - 1)(n + 2)}{2}$ 。
|
|
|
|
|
- **空间复杂度 $O(1)$、原地排序**:指针 $i$ 和 $j$ 使用常数大小的额外空间。
|
2023-08-21 19:33:45 +08:00
|
|
|
|
- **非稳定排序**:如下图所示,元素 `nums[i]` 有可能被交换至与其相等的元素的右边,导致两者相对顺序发生改变。
|
2023-05-24 00:35:46 +08:00
|
|
|
|
|
2023-05-24 00:44:55 +08:00
|
|
|
|
![选择排序非稳定示例](selection_sort.assets/selection_sort_instability.png)
|