MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / selection_sort

Function selection_sort

publication/code/chapter07/selection_sort.rs:3–19  ·  view source on GitHub ↗
(nums: &mut Vec<i32>)

Source from the content-addressed store, hash-verified

1// selection_sort.rs
2
3fn selection_sort(nums: &mut Vec<i32>) {
4 // 待排序数据个数
5 let mut left = nums.len() - 1;
6 while left > 0 {
7 let mut pos_max = 0;
8 for i in 1..=left {
9 if nums[i] > nums[pos_max] {
10 // 当前轮次最大值下标
11 pos_max = i;
12 }
13 }
14
15 // 数据交换,完成一个数据的排序,待排序数据量减 1
16 nums.swap(left, pos_max);
17 left -= 1;
18 }
19}
20
21fn main() {
22 let mut nums = vec![54,32,99,18,75,31,43,56,21,22];

Callers 1

mainFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected