forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cycle_sort.rs
53 lines (51 loc) · 1.48 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
// sorts with the minimum number of rewrites. Runs through all values in the array, placing them in their correct spots. O(n^2).
pub fn cycle_sort(arr: &mut [i32]) {
for cycle_start in 0..arr.len() {
let mut item = arr[cycle_start];
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);
}
}
}
#[cfg(test)]
mod tests {
use super::super::is_sorted;
use super::*;
#[test]
fn it_works() {
let mut arr1 = [6, 5, 4, 3, 2, 1];
cycle_sort(&mut arr1);
assert!(is_sorted(&arr1));
arr1 = [12, 343, 21, 90, 3, 21];
cycle_sort(&mut arr1);
assert!(is_sorted(&arr1));
let mut arr2 = [1];
cycle_sort(&mut arr2);
assert!(is_sorted(&arr2));
let mut arr3 = [213, 542, 90, -23412, -32, 324, -34, 3324, 54];
cycle_sort(&mut arr3);
assert!(is_sorted(&arr3));
}
}