-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbogosort.rs
44 lines (40 loc) · 1.38 KB
/
bogosort.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
extern crate rand;
/// This trait provides the `bogosort` functionality.
pub trait Bogosort {
/// **Bogosort** or **random sort** is a highly ineffective sorting algorithm based on
/// the _"generate and test"_ paradigm. The sorting function permutes its input and
/// then checks whether the generated sequence is correctly ordered.
///
/// If not, a new permutation is generated:
///
/// ```text
/// while not isInOrder(deck):
/// shuffle(deck)
/// ```
/// The permutations can either be enumerated in order to avoid duplicates or
/// completely randomized.
fn bogosort(&mut self);
}
/// This is a mere helper function to determine, whether a vector of objects is sorted.
fn is_sorted<T: PartialOrd>(vec: &Vec<T>) -> bool {
for i in 1..vec.len() {
if !vec[i].ge(&vec[i - 1]) {
return false;
}
}
true
}
/// The trait implementation for the `Vec<T>` type is based on the randomized approach
/// that randomly permutes the function input.
impl<T: PartialOrd> Bogosort for Vec<T> {
fn bogosort(&mut self) {
while !is_sorted(self) {
let mut new_vec: Vec<T> = Vec::new();
while self.len() > 0 {
let length = self.len();
new_vec.push(self.remove(rand::random::<usize>() % length));
}
self.append(&mut new_vec);
}
}
}