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

Function quick_sort2

publication/code/chapter07/quick_sort.rs:43–68  ·  view source on GitHub ↗

分割点不单独计算的快速排序,lm 和 rm 作分割点

(nums: &mut [i32], low: usize, high: usize)

Source from the content-addressed store, hash-verified

41
42// 分割点不单独计算的快速排序,lm 和 rm 作分割点
43fn quick_sort2(nums: &mut [i32], low: usize, high: usize) {
44 if low >= high { return; }
45
46 let mut lm = low;
47 let mut rm = high;
48 while lm < rm {
49 // 右标记不断左移
50 while lm < rm && nums[low] <= nums[rm] {
51 rm -= 1;
52 }
53 // 左标记不断右移
54 while lm < rm && nums[lm] <= nums[low] {
55 lm += 1;
56 }
57 // 交换左右标记处数据
58 nums.swap(lm, rm);
59 }
60 // 交换分割点数据
61 nums.swap(low, lm);
62
63 if lm > 1 {
64 quick_sort2(nums, low, lm - 1);
65 }
66
67 quick_sort2(nums, rm + 1, high);
68}
69
70fn main() {
71 let mut nums = [54,26,93,17,77,31,44,55,20];

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected