forked from alexfertel/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbogo_sort.rs
76 lines (62 loc) · 1.67 KB
/
bogo_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
use crate::math::PCG32;
use crate::sorting::traits::Sorter;
use std::time::{SystemTime, UNIX_EPOCH};
const DEFAULT: u64 = 2 << 31; // 2^32
fn is_sorted<T: Ord>(arr: &[T], len: usize) -> bool {
if len <= 1 {
return true;
}
for i in 0..len - 1 {
if arr[i] > arr[i + 1] {
return false;
}
}
true
}
#[cfg(target_pointer_width = "64")]
fn generate_index(range: usize, generator: &mut PCG32) -> usize {
generator.get_u64() as usize % range
}
#[cfg(not(target_pointer_width = "64"))]
fn generate_index(range: usize, generator: &mut PCG32) -> usize {
generator.get_u32() as usize % range
}
/**
* Fisher–Yates shuffle for generating random permutation.
*/
fn permute_randomly<T>(arr: &mut [T], len: usize, generator: &mut PCG32) {
if len <= 1 {
return;
}
for i in (1..len).rev() {
let j = generate_index(i + 1, generator);
arr.swap(i, j);
}
}
fn bogo_sort<T: Ord>(arr: &mut [T]) {
let seed = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(duration) => duration.as_millis() as u64,
Err(_) => DEFAULT,
};
let mut random_generator = PCG32::new_default(seed);
let arr_length = arr.len();
while !is_sorted(arr, arr_length) {
permute_randomly(arr, arr_length, &mut random_generator);
}
}
pub struct BogoSort;
impl<T> Sorter<T> for BogoSort
where
T: Ord + Copy,
{
fn sort_inplace(arr: &mut [T]) {
bogo_sort(arr);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::BogoSort;
sorting_tests!(BogoSort::sort, bogo_sort);
sorting_tests!(BogoSort::sort_inplace, bogo_sort, inplace);
}