BeepSortRust/src/Algorithm.rs

77 lines
2 KiB
Rust
Raw Normal View History

2022-11-06 21:44:59 +00:00
use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState};
use std::rc::Rc;
use std::thread::yield_now;
#[derive(Debug, Clone)]
pub struct Algorithm{
name:String,
}
impl Algorithm {
pub fn new() -> Algorithm{
Algorithm{name:"Test".to_owned()}
}
pub fn insertSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
2022-11-06 21:44:59 +00:00
move ||{
yield list.clone();
for index in 0..list.len(){
2022-11-06 22:10:16 +00:00
let mut j = index;
while j>0 && list.lessThan(j, j-1){
//yield list.clone();
yield list.swap(j, j-1);
//yield list.clone();
2022-11-06 22:10:16 +00:00
j = j-1;
}
}
2022-11-06 21:44:59 +00:00
}
}
pub fn stalinSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
move ||{
yield list.clone();
let mut cur = 1;
loop{
if cur == list.len() {
break;
}
yield list.clone();
if list.lessThan(cur, cur-1){
list.delete(cur)
}else{
cur += 1;
}
yield list.clone();
}
}
}
2022-11-06 21:44:59 +00:00
2022-11-07 00:44:35 +00:00
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();
}
}
}
2022-11-06 21:44:59 +00:00
}