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 bogosort<T,F>(coll: &mut [T], order: F)
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(coll, &order) {
while !is_sorted(&order, coll) {
rng.shuffle(coll);
rng.shuffle(coll);
}
}
}
}


#[inline]
fn is_sorted<T,F>(coll: &[T], order: F) -> bool
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(&mut testlist, |x,y| x < y);
bogosort_by(|x,y| x < y, &mut testlist);
println!("{:?}", testlist)
println!("{:?}", testlist)
bogosort(&mut testlist, |x,y| x > y);
bogosort_by(|x,y| x > y, &mut testlist);
println!("{:?}", testlist);
println!("{:?}", testlist);
}
}