(nums: &mut Vec<i32>)
| 1 | // selection_sort.rs |
| 2 | |
| 3 | fn 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 | |
| 21 | fn main() { |
| 22 | let mut nums = vec![54,32,99,18,75,31,43,56,21,22]; |