-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.rs
65 lines (56 loc) · 1.55 KB
/
merge_sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 实现归并排序
pub fn merge_sort(mut nums: Vec<i32>) -> Vec<i32> {
let len = nums.len();
if len <= 1 {
return nums;
}
// 递归调用归并排序函数
merge_sort_recursion(&mut nums, 0, len - 1);
nums
}
// 归并函数,将两个有序数组合并成一个有序数组
fn merge(nums: &mut Vec<i32>, left: usize, middle: usize, right: usize) {
// 初始化三个指针
let mut i = left;
let mut j = middle + 1;
let mut k = left;
// 创建临时数组
let mut temp = vec![];
// 将两个有序数组合并到临时数组中
while k <= right {
if i > middle {
temp.push(nums[j]);
j += 1;
k += 1;
} else if j > right {
temp.push(nums[i]);
i += 1;
k += 1;
} else if nums[i] < nums[j] {
temp.push(nums[i]);
i += 1;
k += 1;
} else {
temp.push(nums[j]);
j += 1;
k += 1;
}
}
// 将临时数组中的元素赋值回原数组
for i in 0..=(right - left) {
nums[left + i] = temp[i];
}
}
// 归并排序递归函数
fn merge_sort_recursion(nums: &mut Vec<i32>, left: usize, right: usize) {
if left >= right {
return;
}
// 计算数组中间位置
let middle = (left + right) >> 1;
// 递归调用归并排序函数
merge_sort_recursion(nums, left, middle);
merge_sort_recursion(nums, middle + 1, right);
// 合并两个有序数组
merge(nums, left, middle, right);
}