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

Function bubble_sort3

publication/code/chapter07/bubble_sort.rs:30–43  ·  view source on GitHub ↗
(nums: &mut [i32])

Source from the content-addressed store, hash-verified

28}
29
30fn bubble_sort3(nums: &mut [i32]) {
31 let mut compare = true;
32 let mut len = nums.len() - 1;
33 while len > 0 && compare {
34 compare = false;
35 for i in 0..len {
36 if nums[i] > nums[i+1] {
37 nums.swap(i, i+1);
38 compare = true; // 数据无序,还需继续比较
39 }
40 }
41 len -= 1;
42 }
43}
44
45fn main() {
46 let mut nums = [54,26,93,17,77,31,44,55,20];

Callers 1

mainFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected