use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng};
use std::{cmp::Ordering, iter::from_generator};
pub fn bogo_sort<T>(v: &[T]) -> impl Iterator<Item = Vec<T>>
where
T: PartialOrd + Clone,
{
bogo_sort_by(v, |a, b| a.partial_cmp(b))
}
pub fn bogo_sort_by<T, F>(v: &[T], compare: F) -> impl Iterator<Item = Vec<T>>
where
F: Fn(&T, &T) -> Option<Ordering> + Copy,
T: Clone,
{
let mut state = v.to_vec();
let mut rng = SmallRng::seed_from_u64(state.len() as u64);
from_generator(move || {
yield state.to_vec();
while !state.is_sorted_by(compare) {
state.shuffle(&mut rng);
yield state.to_vec();
}
})
}