-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathgnome_sort.rs
More file actions
40 lines (34 loc) · 774 Bytes
/
gnome_sort.rs
File metadata and controls
40 lines (34 loc) · 774 Bytes
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
use crate::sorting::traits::Sorter;
fn gnome_sort<T: Ord>(arr: &mut [T]) {
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;
}
}
}
}
pub struct GnomeSort;
impl<T> Sorter<T> for GnomeSort
where
T: Ord + Copy,
{
fn sort_inplace(arr: &mut [T]) {
gnome_sort(arr);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::GnomeSort;
sorting_tests!(GnomeSort::sort, gnome_sort);
sorting_tests!(GnomeSort::sort_inplace, gnome_sort, inplace);
}