From 5203cf1359be19cf4fde49724c0d9eaa8ef324d6 Mon Sep 17 00:00:00 2001 From: polsevev Date: Sun, 6 Nov 2022 23:10:16 +0100 Subject: [PATCH] Created InsertSort --- Cargo.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 2 +- src/Algorithm.rs | 17 +++++++++------- src/GuiHookVec.rs | 2 +- src/main.rs | 3 ++- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2182ab2..8ac08ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "BeepSortMacroQuad" version = "0.1.0" dependencies = [ + "beep", "macroquad", ] @@ -51,10 +52,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] -name = "bitflags" -version = "1.3.2" +name = "beep" +version = "0.3.0" 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]] name = "bumpalo" @@ -74,6 +85,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cc" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574" + [[package]] name = "cfg-if" version = "1.0.0" @@ -176,6 +193,12 @@ dependencies = [ "png", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "lewton" version = "0.9.4" @@ -230,6 +253,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "miniquad" version = "0.3.14" @@ -266,6 +298,19 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "num-integer" version = "0.1.45" diff --git a/Cargo.toml b/Cargo.toml index a344a16..595bb1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" [dependencies] macroquad = "0.3.25" - +beep = "0.3.0" [toolchain] channel = "nightly" diff --git a/src/Algorithm.rs b/src/Algorithm.rs index 31fc037..8d284b9 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -4,7 +4,7 @@ use crate::GuiHookVec::GuiVec; use std::ops::{Generator, GeneratorState}; use std::rc::Rc; use std::thread::yield_now; - +use beep::beep; #[derive(Debug, Clone)] pub struct Algorithm{ name:String, @@ -19,12 +19,15 @@ impl Algorithm { pub fn sort<'a>(&'a self, list: &'a mut GuiVec) -> impl Generator +'a{ move ||{ yield list.clone(); - list.swap(1, 10); - yield list.clone(); - list.swap(5, 15); - yield list.clone(); - list.randomize(); - yield list.clone(); + + for index in 0..list.clone().len(){ + let mut j = index; + while j>0 && list.get(j-1).position > list.get(j).position{ + list.swap(j, j-1); + yield list.clone(); + j = j-1; + } + } } } diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index e59c858..809c25e 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -22,7 +22,7 @@ pub struct GuiVec{ impl GuiVec{ pub fn new(length:i32) -> Self { let mut list:Vec = vec!(); - for i in 0..length { + for i in 1..length+1 { list.push(Bar::new(i)); } GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new()} diff --git a/src/main.rs b/src/main.rs index ca043ca..ca73d60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use crate::GuiHookVec::GuiVec; use crate::StateMover::State; use std::ops::{Generator, GeneratorState}; + const BAR_WIDTH:f32 = 10.0; #[macroquad::main("BeepSort")] async fn main() { @@ -26,7 +27,7 @@ async fn main() { loop { - if get_time()-lasttime > 1.0 && !finished{ + if get_time()-lasttime > 0.005 && !finished{ match Pin::new(& mut generator).resume(()){ GeneratorState::Yielded(x) => { holder = x.clone();