forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_sort.rs
153 lines (135 loc) · 4.37 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
fn merge<T: Ord + Copy>(arr: &mut [T], mid: usize) {
// Create temporary vectors to support the merge.
let left_half = arr[..mid].to_vec();
let right_half = arr[mid..].to_vec();
// Indexes to track the positions while merging.
let mut l = 0;
let mut r = 0;
for v in arr {
// Choose either the smaller element, or from whichever vec is not exhausted.
if r == right_half.len() || (l < left_half.len() && left_half[l] < right_half[r]) {
*v = left_half[l];
l += 1;
} else {
*v = right_half[r];
r += 1;
}
}
}
pub fn top_down_merge_sort<T: Ord + Copy>(arr: &mut [T]) {
if arr.len() > 1 {
let mid = arr.len() / 2;
// Sort the left half recursively.
top_down_merge_sort(&mut arr[..mid]);
// Sort the right half recursively.
top_down_merge_sort(&mut arr[mid..]);
// Combine the two halves.
merge(arr, mid);
}
}
pub fn bottom_up_merge_sort<T: Copy + Ord>(a: &mut [T]) {
if a.len() > 1 {
let len: usize = a.len();
let mut sub_array_size: usize = 1;
while sub_array_size < len {
let mut start_index: usize = 0;
// still have more than one sub-arrays to merge
while len - start_index > sub_array_size {
let end_idx: usize = if start_index + 2 * sub_array_size > len {
len
} else {
start_index + 2 * sub_array_size
};
// merge a[start_index..start_index+sub_array_size] and a[start_index+sub_array_size..end_idx]
// NOTE: mid is a relative index number starting from `start_index`
merge(&mut a[start_index..end_idx], sub_array_size);
// update `start_index` to merge the next sub-arrays
start_index = end_idx;
}
sub_array_size *= 2;
}
}
}
#[cfg(test)]
mod tests {
#[cfg(test)]
mod top_down_merge_sort {
use super::super::*;
#[test]
fn basic() {
let mut res = vec![10, 8, 4, 3, 1, 9, 2, 7, 5, 6];
top_down_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
#[test]
fn basic_string() {
let mut res = vec!["a", "bb", "d", "cc"];
top_down_merge_sort(&mut res);
assert_eq!(res, vec!["a", "bb", "cc", "d"]);
}
#[test]
fn empty() {
let mut res = Vec::<u8>::new();
top_down_merge_sort(&mut res);
assert_eq!(res, vec![]);
}
#[test]
fn one_element() {
let mut res = vec![1];
top_down_merge_sort(&mut res);
assert_eq!(res, vec![1]);
}
#[test]
fn pre_sorted() {
let mut res = vec![1, 2, 3, 4];
top_down_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
#[test]
fn reverse_sorted() {
let mut res = vec![4, 3, 2, 1];
top_down_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
}
#[cfg(test)]
mod bottom_up_merge_sort {
use super::super::*;
#[test]
fn basic() {
let mut res = vec![10, 8, 4, 3, 1, 9, 2, 7, 5, 6];
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
#[test]
fn basic_string() {
let mut res = vec!["a", "bb", "d", "cc"];
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec!["a", "bb", "cc", "d"]);
}
#[test]
fn empty() {
let mut res = Vec::<u8>::new();
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec![]);
}
#[test]
fn one_element() {
let mut res = vec![1];
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec![1]);
}
#[test]
fn pre_sorted() {
let mut res = vec![1, 2, 3, 4];
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
#[test]
fn reverse_sorted() {
let mut res = vec![4, 3, 2, 1];
bottom_up_merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
}
}