Created InsertSort

This commit is contained in:
Rolf Martin Glomsrud 2022-11-06 23:10:16 +01:00
parent 8d5847e1d9
commit 5203cf1359
5 changed files with 62 additions and 13 deletions

51
Cargo.lock generated
View file

@ -6,6 +6,7 @@ version = 3
name = "BeepSortMacroQuad" name = "BeepSortMacroQuad"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"beep",
"macroquad", "macroquad",
] ]
@ -51,10 +52,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "bitflags" name = "beep"
version = "1.3.2" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "add99ab8e6fa29e525696f04be01c6e18815f5d799e026a06c8b09af8301bd5a"
dependencies = [
"lazy_static",
"nix",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
@ -74,6 +85,12 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cc"
version = "1.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -176,6 +193,12 @@ dependencies = [
"png", "png",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "lewton" name = "lewton"
version = "0.9.4" version = "0.9.4"
@ -230,6 +253,15 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "miniquad" name = "miniquad"
version = "0.3.14" version = "0.3.14"
@ -266,6 +298,19 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121" checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121"
[[package]]
name = "nix"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5e06129fb611568ef4e868c14b326274959aa70ff7776e9d55323531c374945"
dependencies = [
"bitflags",
"cc",
"cfg-if",
"libc",
"memoffset",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.45" version = "0.1.45"

View file

@ -7,6 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
macroquad = "0.3.25" macroquad = "0.3.25"
beep = "0.3.0"
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"

View file

@ -4,7 +4,7 @@ use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState}; use std::ops::{Generator, GeneratorState};
use std::rc::Rc; use std::rc::Rc;
use std::thread::yield_now; use std::thread::yield_now;
use beep::beep;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Algorithm{ pub struct Algorithm{
name:String, name:String,
@ -19,12 +19,15 @@ impl Algorithm {
pub fn sort<'a>(&'a self, list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{ pub fn sort<'a>(&'a self, list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
move ||{ move ||{
yield list.clone(); yield list.clone();
list.swap(1, 10);
yield list.clone(); for index in 0..list.clone().len(){
list.swap(5, 15); let mut j = index;
yield list.clone(); while j>0 && list.get(j-1).position > list.get(j).position{
list.randomize(); list.swap(j, j-1);
yield list.clone(); yield list.clone();
j = j-1;
}
}
} }
} }

View file

@ -22,7 +22,7 @@ pub struct GuiVec{
impl GuiVec{ impl GuiVec{
pub fn new(length:i32) -> Self { pub fn new(length:i32) -> Self {
let mut list:Vec<Bar> = vec!(); let mut list:Vec<Bar> = vec!();
for i in 0..length { for i in 1..length+1 {
list.push(Bar::new(i)); list.push(Bar::new(i));
} }
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new()} GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new()}

View file

@ -13,6 +13,7 @@ use crate::GuiHookVec::GuiVec;
use crate::StateMover::State; use crate::StateMover::State;
use std::ops::{Generator, GeneratorState}; 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() {
@ -26,7 +27,7 @@ async fn main() {
loop { loop {
if get_time()-lasttime > 1.0 && !finished{ if get_time()-lasttime > 0.005 && !finished{
match Pin::new(& mut generator).resume(()){ match Pin::new(& mut generator).resume(()){
GeneratorState::Yielded(x) => { GeneratorState::Yielded(x) => {
holder = x.clone(); holder = x.clone();