added some more sorting algos

This commit is contained in:
Rolf Martin Glomsrud 2022-11-07 01:44:35 +01:00
parent fb91932372
commit 8bd2b12103
3 changed files with 40 additions and 2 deletions

View file

@ -48,4 +48,30 @@ impl Algorithm {
} }
} }
pub fn bubbleSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a {
move || {
let n = list.len();
for i in 0..n {
for j in 0..(n - i - 1) {
if list.lessThan(j + 1, j) {
yield list.swap(j, j + 1);
}
}
}
}
}
pub fn bogoSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
move || {
loop{
yield list.clone();
if list.isSorted() {
break;
}
list.randomize();
}
}
}
} }

View file

@ -86,5 +86,17 @@ impl GuiVec{
self.comps += 1; self.comps += 1;
return self.get(a).position < self.get(b).position return self.get(a).position < self.get(b).position
} }
pub fn isSorted(&mut self) -> bool{
self.reads += self.len() as i32;
let mut prev = 0;
for bar in self.list.iter() {
if bar.position < prev{
return false;
}else{
prev = bar.position;
}
}
true
}
} }

View file

@ -16,11 +16,11 @@ use std::ops::{Generator, GeneratorState};
const BAR_WIDTH:f32 = 10.0; const BAR_WIDTH:f32 = 10.0;
#[macroquad::main("BeepSort")] #[macroquad::main("BeepSort")]
async fn main() { async fn main() {
let mut gui_vec = GuiVec::new(50); let mut gui_vec = GuiVec::new(5);
gui_vec.randomize(); gui_vec.randomize();
let mut lasttime:f64 = 0.; let mut lasttime:f64 = 0.;
let mut holder = gui_vec.clone(); let mut holder = gui_vec.clone();
let mut generator = Algorithm::Algorithm::insertSort(&mut gui_vec); let mut generator = Algorithm::Algorithm::bogoSort(&mut gui_vec);
let mut finished = false; let mut finished = false;
let timeout = 0.000001; let timeout = 0.000001;