From b576638128a87f003247e4418541358bdf9159c1 Mon Sep 17 00:00:00 2001 From: polsevev Date: Tue, 28 Feb 2023 23:40:08 +0100 Subject: [PATCH] broken quicksort, async recursion is not being friendly --- Cargo.lock | 76 +++++++++++++++++++++++++++++++++++------------ Cargo.toml | 2 +- src/Algorithm.rs | 46 +++++++++++++++++++++++++++- src/GuiHookVec.rs | 4 +-- src/main.rs | 7 +++++ 5 files changed, 112 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 699f7a7..b4a9a38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 3 name = "BeepSortMacroQuad" version = "0.1.0" dependencies = [ + "async-recursion", "macroquad", ] @@ -26,6 +27,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "async-recursion" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b015a331cc64ebd1774ba119538573603427eaace0a1950c423ab971f903796" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "audir-sles" version = "0.1.0" @@ -52,9 +64,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" @@ -112,12 +124,12 @@ checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" [[package]] name = "flate2" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" dependencies = [ "crc32fast", - "miniz_oxide 0.5.4", + "miniz_oxide", ] [[package]] @@ -164,9 +176,9 @@ checksum = "4d13cdbd5dbb29f9c88095bbdc2590c9cba0d0a1269b983fef6b2cdd7e9f4db1" [[package]] name = "image" -version = "0.24.4" +version = "0.24.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd8e4fb07cf672b1642304e731ef8a6a4c7891d67bb4fd4f5ce58cd6ed86803c" +checksum = "69b7ea949b537b0fd0af141fff8c77690f2ce96f4f41f042ccb6c69c6c965945" dependencies = [ "bytemuck", "byteorder", @@ -242,15 +254,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "miniz_oxide" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" -dependencies = [ - "adler", -] - [[package]] name = "miniz_oxide" version = "0.6.2" @@ -329,7 +332,16 @@ dependencies = [ "bitflags", "crc32fast", "flate2", - "miniz_oxide 0.6.2", + "miniz_oxide", +] + +[[package]] +name = "proc-macro2" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +dependencies = [ + "unicode-ident", ] [[package]] @@ -349,9 +361,9 @@ checksum = "658fa1faf7a4cc5f057c9ee5ef560f717ad9d8dc66d975267f709624d6e1ab88" [[package]] name = "quad-snd" -version = "0.2.5" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e82e2e4a55292a75d8569ef0b3f7c24964074efe5767b359dbf028a0b3c53464" +checksum = "53c954bb70493a2872775b74b663a767686e6d96d242e789d6a92cc4ebd2a64e" dependencies = [ "audir-sles", "audrey", @@ -360,6 +372,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + [[package]] name = "smallvec" version = "0.6.14" @@ -369,12 +390,29 @@ dependencies = [ "maybe-uninit", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "ttf-parser" version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd" +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 6952ddd..b126d86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] macroquad = "0.3.25" - +async-recursion = "1.0.2" [toolchain] channel = "nightly" [profile.release] diff --git a/src/Algorithm.rs b/src/Algorithm.rs index 646c56c..09bdf8e 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -3,11 +3,13 @@ use crate::BarPlugin::Bar; use crate::GuiHookVec::GuiVec; +use async_recursion::async_recursion; use macroquad::prelude::screen_width; use macroquad::window::screen_height; use std::collections::BinaryHeap; use std::collections::HashMap; + #[derive(Debug, Clone)] pub struct Algorithm{ @@ -25,6 +27,7 @@ impl Algorithm{ "bogoSort" => Algorithm::bogoSort(&mut list).await, "cocktailShaker" => Algorithm::cocktailShaker(&mut list).await, "binaryHeap" => Algorithm::binaryHeap(&mut list).await, + "quickSort" => Algorithm::quickSort(&mut list, 0, length as usize -1).await, _ => panic!("No algorithm with that name implemented!") } @@ -141,7 +144,48 @@ impl Algorithm{ } - + + + pub async fn partition(list:&mut GuiVec, mut low:usize, mut high:usize, p:i32) -> i32{ + while low <= high{ + while list.list[low].position < p{ + low += 1; + } + while list.list[high].position > p{ + high -= 1; + } + if low <= high{ + if list.swap(low, high).await {return -1}; + low+=1; + if high == 0{ + return low as i32 + } + high-=1; + } + } + low as i32 + } + + #[async_recursion] + pub async fn quickSort(list:&mut GuiVec, low:usize, high:usize) { + if low>=high{ + return; + } + + let p = list.list[0].position; + let mut index = 0; + let temp = Algorithm::partition(list, low, high, p).await; + if temp < 0{ + return + }else{ + index = temp as usize + } + + Algorithm::quickSort(list, low, index-1).await; + Algorithm::quickSort(list, index, high).await; + + + } diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index 703911d..cc87cb3 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -18,7 +18,7 @@ use crate::Algorithm::Algorithm; #[derive(Clone, Debug)] pub struct GuiVec{ - list: Vec, + pub list: Vec, initialSize:usize, pub lastTime:f64, pub reads:i32, @@ -74,7 +74,7 @@ impl GuiVec{ for (count,bar) in self.list.iter().enumerate(){ draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)),screen_height() - (screen_height()/((self.len()) as f32))*bar.position as f32 , screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32, bar.color); - + } draw_text(&format!("FPS: {}", get_fps()), screen_width()*0.01 + 40., 80.0, 20.0, BLACK); diff --git a/src/main.rs b/src/main.rs index 3fc4e86..fec5bdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod Algorithm; use macroquad::prelude::*; use macroquad::prelude::scene::clear; + use crate::BarPlugin::Bar; use crate::GuiHookVec::GuiVec; @@ -21,6 +22,9 @@ async fn main() { loop{ clear_background(WHITE); + + + delay = match delayText.parse::(){ Ok(a) => a/1000., Err(error)=> {0.1} @@ -53,6 +57,9 @@ async fn main() { if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "Run BogoSort!"){ algo = "bogoSort"; } + if root_ui().button(Vec2::new(screen_width()*0.01, 250.), "Run QuickSort!"){ + algo = "quickSort"; + } if algo != ""{ Algorithm::Algorithm::run(length, 1.0, algo.to_string()).await;