Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
(→{{header|Rust}}: Made calls a little more intuitive) |
|||
Line 2,200: | Line 2,200: | ||
use rand::Rng; |
use rand::Rng; |
||
fn |
fn bogosort_by<T,F>(order: F, coll: &mut [T]) |
||
where F: Fn(&T, &T) -> bool |
where F: Fn(&T, &T) -> bool |
||
{ |
{ |
||
let mut rng = rand::thread_rng(); |
let mut rng = rand::thread_rng(); |
||
while !is_sorted( |
while !is_sorted(&order, coll) { |
||
rng.shuffle(coll); |
rng.shuffle(coll); |
||
} |
} |
||
} |
} |
||
#[inline] |
|||
fn |
fn is_sorted_by<T,F>(order: F, coll: &[T]) -> bool |
||
where F: Fn(&T,&T) -> bool, |
where F: Fn(&T,&T) -> bool, |
||
{ |
{ |
||
Line 2,218: | Line 2,219: | ||
fn main() { |
fn main() { |
||
let mut testlist = [1,55,88,24,990876,312,67,0,854,13,4,7]; |
let mut testlist = [1,55,88,24,990876,312,67,0,854,13,4,7]; |
||
bogosort_by(|x,y| x < y, &mut testlist); |
|||
println!("{:?}", testlist) |
println!("{:?}", testlist) |
||
bogosort_by(|x,y| x > y, &mut testlist); |
|||
println!("{:?}", testlist); |
println!("{:?}", testlist); |
||
} |
} |