-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
quick_sort.rs
137 lines (119 loc) · 3.45 KB
/
quick_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
pub fn partition<T: PartialOrd>(arr: &mut [T], lo: usize, hi: usize) -> usize {
let pivot = hi;
let mut i = lo;
let mut j = hi - 1;
loop {
while arr[i] < arr[pivot] {
i += 1;
}
while j > 0 && arr[j] > arr[pivot] {
j -= 1;
}
if j == 0 || i >= j {
break;
} else if arr[i] == arr[j] {
i += 1;
j -= 1;
} else {
arr.swap(i, j);
}
}
arr.swap(i, pivot);
i
}
fn _quick_sort<T: Ord>(arr: &mut [T], mut lo: usize, mut hi: usize) {
while lo < hi {
let pivot = partition(arr, lo, hi);
if pivot - lo < hi - pivot {
if pivot > 0 {
_quick_sort(arr, lo, pivot - 1);
}
lo = pivot + 1;
} else {
_quick_sort(arr, pivot + 1, hi);
hi = pivot - 1;
}
}
}
pub fn quick_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
if len > 1 {
_quick_sort(arr, 0, len - 1);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sorting::have_same_elements;
use crate::sorting::is_sorted;
use crate::sorting::sort_utils;
#[test]
fn basic() {
let mut res = vec![10, 8, 4, 3, 1, 9, 2, 7, 5, 6];
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn basic_string() {
let mut res = vec!["a", "bb", "d", "cc"];
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn empty() {
let mut res = Vec::<u8>::new();
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn one_element() {
let mut res = vec![1];
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn pre_sorted() {
let mut res = vec![1, 2, 3, 4];
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn reverse_sorted() {
let mut res = vec![4, 3, 2, 1];
let cloned = res.clone();
quick_sort(&mut res);
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn large_elements() {
let mut res = sort_utils::generate_random_vec(300000, 0, 1000000);
let cloned = res.clone();
sort_utils::log_timed("large elements test", || {
quick_sort(&mut res);
});
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn nearly_ordered_elements() {
let mut res = sort_utils::generate_nearly_ordered_vec(3000, 10);
let cloned = res.clone();
sort_utils::log_timed("nearly ordered elements test", || {
quick_sort(&mut res);
});
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
#[test]
fn repeated_elements() {
let mut res = sort_utils::generate_repeated_elements_vec(1000000, 3);
let cloned = res.clone();
sort_utils::log_timed("repeated elements test", || {
quick_sort(&mut res);
});
assert!(is_sorted(&res) && have_same_elements(&res, &cloned));
}
}