need to implement in place version of radix sort
This commit is contained in:
parent
d151bfb71c
commit
c7b41c94c3
4 changed files with 64 additions and 1 deletions
|
@ -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::<Vec<char>>();
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue