-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
gnome_sort.rs
67 lines (59 loc) · 1.6 KB
/
gnome_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
use std::cmp;
pub fn gnome_sort<T>(arr: &[T]) -> Vec<T>
where
T: cmp::PartialEq + cmp::PartialOrd + Clone,
{
let mut arr = arr.to_vec();
let mut i: usize = 1;
let mut j: usize = 2;
while i < arr.len() {
if arr[i - 1] < arr[i] {
i = j;
j = i + 1;
} else {
arr.swap(i - 1, i);
i -= 1;
if i == 0 {
i = j;
j += 1;
}
}
}
arr
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sorting::have_same_elements;
use crate::sorting::is_sorted;
#[test]
fn basic() {
let original = [6, 5, -8, 3, 2, 3];
let res = gnome_sort(&original);
assert!(is_sorted(&res) && have_same_elements(&res, &original));
}
#[test]
fn already_sorted() {
let original = gnome_sort(&["a", "b", "c"]);
let res = gnome_sort(&original);
assert!(is_sorted(&res) && have_same_elements(&res, &original));
}
#[test]
fn odd_number_of_elements() {
let original = gnome_sort(&["d", "a", "c", "e", "b"]);
let res = gnome_sort(&original);
assert!(is_sorted(&res) && have_same_elements(&res, &original));
}
#[test]
fn one_element() {
let original = gnome_sort(&[3]);
let res = gnome_sort(&original);
assert!(is_sorted(&res) && have_same_elements(&res, &original));
}
#[test]
fn empty() {
let original = gnome_sort(&Vec::<u8>::new());
let res = gnome_sort(&original);
assert!(is_sorted(&res) && have_same_elements(&res, &original));
}
}