Add the section of selection sort. (#513)
35
codes/cpp/chapter_sorting/selection_sort.cpp
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/**
|
||||||
|
* File: selection_sort.cpp
|
||||||
|
* Created Time: 2023-05-23
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../utils/common.hpp"
|
||||||
|
|
||||||
|
/* 选择排序 */
|
||||||
|
void selectionSort(vector<int> &nums) {
|
||||||
|
int n = nums.size();
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间 [i, n-1] 中的最小元素
|
||||||
|
int k = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (nums[j] < nums[k]) {
|
||||||
|
k = j; // 更新最小元素
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将该最小元素与未排序区间的首个元素交换
|
||||||
|
swap(nums[i], nums[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
int main() {
|
||||||
|
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||||
|
selectionSort(nums);
|
||||||
|
|
||||||
|
cout << "选择排序完成后 nums = ";
|
||||||
|
printVector(nums);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
36
codes/java/chapter_sorting/selection_sort.java
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
* File: selection_sort.java
|
||||||
|
* Created Time: 2023-05-23
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package chapter_sorting;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class selection_sort {
|
||||||
|
/* 选择排序 */
|
||||||
|
public static void selectionSort(int[] nums) {
|
||||||
|
int n = nums.length;
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间 [i, n-1] 中的最小元素
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||||
|
selectionSort(nums);
|
||||||
|
System.out.println("选择排序完成后 nums = " + Arrays.toString(nums));
|
||||||
|
}
|
||||||
|
}
|
26
codes/python/chapter_sorting/selection_sort.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
"""
|
||||||
|
File: selection_sort.py
|
||||||
|
Created Time: 2023-05-22
|
||||||
|
Author: Krahets (krahets@163.com)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def selection_sort(nums: list[int]):
|
||||||
|
"""选择排序"""
|
||||||
|
n = len(nums)
|
||||||
|
# 外循环:未排序区间为 [i, n-1]
|
||||||
|
for i in range(n - 1):
|
||||||
|
# 内循环:找到未排序区间 [i, 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]
|
||||||
|
|
||||||
|
|
||||||
|
"""Driver Code"""
|
||||||
|
if __name__ == "__main__":
|
||||||
|
nums = [4, 1, 3, 1, 5, 2]
|
||||||
|
selection_sort(nums)
|
||||||
|
print("选择排序完成后 nums =", nums)
|
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 53 KiB |
112
docs/chapter_sorting/selection_sort.md
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
# 选择排序
|
||||||
|
|
||||||
|
「选择排序 Insertion Sort」的工作原理非常直接:开启一个循环,每轮从未排序区间选择最小的元素,将其放到已排序区间的末尾。完整步骤如下:
|
||||||
|
|
||||||
|
1. 初始状态下,所有元素未排序,即未排序(索引)区间为 $[0, n-1]$ 。
|
||||||
|
2. 选取区间 $[0, n-1]$ 中的最小元素,将其与索引 $0$ 处元素交换。完成后,数组前 1 个元素已排序。
|
||||||
|
3. 选取区间 $[1, n-1]$ 中的最小元素,将其与索引 $1$ 处元素交换。完成后,数组前 2 个元素已排序。
|
||||||
|
4. 以此类推。经过 $n - 1$ 轮选择与交换后,数组前 $n - 1$ 个元素已排序。
|
||||||
|
5. 仅剩的一个元素必定是最大元素,无需排序,因此数组排序完成。
|
||||||
|
|
||||||
|
=== "<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$ 来记录未排序区间内的最小元素。
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="selection_sort.java"
|
||||||
|
[class]{selection_sort}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="selection_sort.cpp"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="selection_sort.py"
|
||||||
|
[class]{}-[func]{selection_sort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="selection_sort.go"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JavaScript"
|
||||||
|
|
||||||
|
```javascript title="selection_sort.js"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TypeScript"
|
||||||
|
|
||||||
|
```typescript title="selection_sort.ts"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="selection_sort.c"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="selection_sort.cs"
|
||||||
|
[class]{selection_sort}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="selection_sort.swift"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="selection_sort.zig"
|
||||||
|
[class]{}-[func]{selectionSort}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 算法特性
|
||||||
|
|
||||||
|
- **时间复杂度为 $O(n^2)$ 、非自适应排序**:共有 $n - 1$ 轮外循环,分别包含 $n$ , $n - 1$ , $\cdots$ , $2$ , $2$ 轮内循环,求和为 $\frac{(n - 1)(n + 2)}{2}$ 。
|
||||||
|
- **空间复杂度 $O(1)$ 、原地排序**:指针 $i$ , $j$ 使用常数大小的额外空间。
|
||||||
|
- **非稳定排序**:在交换元素时,有可能将 `nums[i]` 交换至其相等元素的右边,导致两者的相对顺序发生改变。
|
||||||
|
|
||||||
|
![选择排序非稳定示例](selection_sort.assets/selection_sort_step11.png)
|
17
mkdocs.yml
|
@ -181,14 +181,15 @@ nav:
|
||||||
- 10.5. 小结: chapter_searching/summary.md
|
- 10.5. 小结: chapter_searching/summary.md
|
||||||
- 11. 排序算法:
|
- 11. 排序算法:
|
||||||
- 11.1. 排序算法: chapter_sorting/sorting_algorithm.md
|
- 11.1. 排序算法: chapter_sorting/sorting_algorithm.md
|
||||||
- 11.2. 冒泡排序: chapter_sorting/bubble_sort.md
|
- 11.2. 选择排序: chapter_sorting/selection_sort.md
|
||||||
- 11.3. 插入排序: chapter_sorting/insertion_sort.md
|
- 11.3. 冒泡排序: chapter_sorting/bubble_sort.md
|
||||||
- 11.4. 快速排序: chapter_sorting/quick_sort.md
|
- 11.4. 插入排序: chapter_sorting/insertion_sort.md
|
||||||
- 11.5. 归并排序: chapter_sorting/merge_sort.md
|
- 11.5. 快速排序: chapter_sorting/quick_sort.md
|
||||||
- 11.6. 桶排序: chapter_sorting/bucket_sort.md
|
- 11.6. 归并排序: chapter_sorting/merge_sort.md
|
||||||
- 11.7. 计数排序: chapter_sorting/counting_sort.md
|
- 11.7. 桶排序: chapter_sorting/bucket_sort.md
|
||||||
- 11.8. 基数排序: chapter_sorting/radix_sort.md
|
- 11.8. 计数排序: chapter_sorting/counting_sort.md
|
||||||
- 11.9. 小结: chapter_sorting/summary.md
|
- 11.9. 基数排序: chapter_sorting/radix_sort.md
|
||||||
|
- 11.10. 小结: chapter_sorting/summary.md
|
||||||
- 12. 回溯算法:
|
- 12. 回溯算法:
|
||||||
- 12.1. 回溯算法(New): chapter_backtracking/backtracking_algorithm.md
|
- 12.1. 回溯算法(New): chapter_backtracking/backtracking_algorithm.md
|
||||||
- 12.2. 全排列问题(New): chapter_backtracking/permutations_problem.md
|
- 12.2. 全排列问题(New): chapter_backtracking/permutations_problem.md
|
||||||
|
|