mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
build
This commit is contained in:
@@ -146,7 +146,22 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="selection_sort.c"
|
||||
[class]{}-[func]{selectionSort}
|
||||
/* 选择排序 */
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -198,7 +213,22 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="selection_sort.dart"
|
||||
[class]{}-[func]{selectionSort}
|
||||
/* 选择排序 */
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 11.2.1. 算法特性
|
||||
|
||||
Reference in New Issue
Block a user