-
Notifications
You must be signed in to change notification settings - Fork 74
/
cycle_sort.rs
53 lines (48 loc) · 1.35 KB
/
cycle_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
use crate::sorting::traits::Sorter;
fn cycle_sort<T: Ord + Clone>(arr: &mut [T]) {
for cycle_start in 0..arr.len() {
let mut item = arr[cycle_start].clone();
let mut pos = cycle_start;
for i in arr.iter().skip(cycle_start + 1) {
if *i < item {
pos += 1;
}
}
if pos == cycle_start {
continue;
}
while item == arr[pos] {
pos += 1;
}
std::mem::swap(&mut arr[pos], &mut item);
while pos != cycle_start {
pos = cycle_start;
for i in arr.iter().skip(cycle_start + 1) {
if *i < item {
pos += 1;
}
}
while item == arr[pos] {
pos += 1;
}
std::mem::swap(&mut arr[pos], &mut item);
}
}
}
// sorts with the minimum number of rewrites. Runs through all values in the array, placing them in their correct spots. O(n^2).
pub struct CycleSort;
impl<T> Sorter<T> for CycleSort
where
T: Ord + Copy + Clone,
{
fn sort_inplace(arr: &mut [T]) {
cycle_sort(arr);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::CycleSort;
sorting_tests!(CycleSort::sort, cycle_sort);
sorting_tests!(CycleSort::sort_inplace, cycle_sort, inplace);
}