From c7b41c94c35206de5de826cb6bf65c05bc2d6a0e Mon Sep 17 00:00:00 2001 From: polsevev Date: Fri, 3 Mar 2023 00:49:41 +0100 Subject: [PATCH] need to implement in place version of radix sort --- src/Algorithm.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ src/BarPlugin.rs | 2 +- src/GuiHookVec.rs | 9 +++++++++ src/main.rs | 3 +++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Algorithm.rs b/src/Algorithm.rs index e7cd251..d5d96bb 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -8,7 +8,9 @@ use macroquad::prelude::screen_width; use macroquad::window::screen_height; use std::collections::BinaryHeap; use std::collections::HashMap; +use std::collections::HashSet; use std::f32::consts::PI; +use std::hash::Hash; #[derive(Debug, Clone)] @@ -21,6 +23,7 @@ impl Algorithm{ pub async fn run(length:i32, delay:f32, functionName:String){ let mut list = GuiVec::new(length, delay); list.randomize(); + match functionName.as_str() { "insertSort" => Algorithm::insertSort(&mut list).await, @@ -29,6 +32,7 @@ impl Algorithm{ "cocktailShaker" => Algorithm::cocktailShaker(&mut list).await, "binaryHeap" => Algorithm::binaryHeap(&mut list).await, "quickSort" => Algorithm::quickSort(&mut list).await, + "radixSort" => Algorithm::radixSort(&mut list).await, _ => panic!("No algorithm with that name implemented!") } @@ -189,6 +193,53 @@ impl Algorithm{ } + pub async fn radixSort(list:&mut GuiVec) { + + let mut max = usize::MAX; + for i in list.list.clone().into_iter().map(|x| x.position.to_string()){ + if max < i.len(){ + max = i.len(); + } + } + + for i in 0..(max){ + if Algorithm::radix(list, i).await {return}; + } + } + + pub async fn radix(list:&mut GuiVec, radix:usize) -> bool{ + let mut bucket = vec![vec![];10]; + + for (i, bar) in list.elements().enumerate(){ + + let cur = bar.position.to_string().chars().rev().collect::>(); + if cur.len() > radix{ + bucket[cur[radix].to_digit(10).unwrap() as usize].push(i); + }else{ + bucket[0].push(i); + } + + } + + + let mut sortedIndexes = Vec::new(); + for elements in bucket.into_iter(){ + for i in elements{ + sortedIndexes.push(i); + } + } + + let mut listClone = list.list.clone(); + let mut count = 0; + for i in sortedIndexes.clone(){ + if list.set(count, listClone[i]).await {return true}; + count += 1; + + } + + false + } + } \ No newline at end of file diff --git a/src/BarPlugin.rs b/src/BarPlugin.rs index b6b0109..83be026 100644 --- a/src/BarPlugin.rs +++ b/src/BarPlugin.rs @@ -2,7 +2,7 @@ use macroquad::color; use macroquad::color_u8; use macroquad::rand; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct Bar { pub position:i32, pub color:color::Color diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index 4ba03b9..7123b09 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -166,6 +166,15 @@ impl GuiVec{ } true } + pub async fn set(&mut self, i:usize, elem:Bar) -> bool{ + + self.writes += 1; + self.reads += 1; + self.list[i] = elem; + self.draw().await; + self.done + + } pub async fn show(&mut self){ loop{ if !self.done{ diff --git a/src/main.rs b/src/main.rs index fec5bdf..4dc96a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,9 @@ async fn main() { if root_ui().button(Vec2::new(screen_width()*0.01, 250.), "Run QuickSort!"){ algo = "quickSort"; } + if root_ui().button(Vec2::new(screen_width()*0.01, 280.), "Run RadixSort!"){ + algo = "radixSort"; + } if algo != ""{ Algorithm::Algorithm::run(length, 1.0, algo.to_string()).await;