From 3e013192d048a10ed1addd9cd809bc4f0714f77b Mon Sep 17 00:00:00 2001 From: polsevev Date: Sat, 4 Mar 2023 04:32:52 +0100 Subject: [PATCH] Added MSB and LSB radix sort algorithms --- src/GuiHookVec.rs | 19 +++---- src/algorithm.rs | 7 ++- src/algorithm/radixSortLSD.rs | 94 +++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 src/algorithm/radixSortLSD.rs diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index aadeaec..e0719f9 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -95,13 +95,7 @@ impl SortingList for GuiVec{ loop { - /* - self.checkPaused(); - if self.isPaused{ - break; - } - */ if self.skipped >= self.renderSkip{ clear_background(WHITE); @@ -114,15 +108,16 @@ impl SortingList for GuiVec{ } - draw_text(&format!("FPS: {}", get_fps()), screen_width()*0.01 + 40., 80.0, 20.0, BLACK); - draw_text(&format!("Array reads: {}", self.reads), screen_width()*0.01 + 40., 110.0, 20.0, BLACK); - draw_text(&format!("Array writes: {}", self.writes), screen_width()*0.01 + 40., 140.0, 20.0, BLACK); - draw_text(&format!("Comparisons: {}", self.comps), screen_width()*0.01 + 40., 170.0, 20.0, BLACK); - + root_ui().window(hash!(),Vec2::new(screen_width()*0.01, 5.), Vec2::new(800.0, 50.), |ui|{ ui.input_text(hash!(), "Delay (ms)", &mut delayText); ui.input_text(hash!(), "StepsPrFrame (How many steps of the algorithm pr frame)", &mut renderSkipText); - + draw_text(&format!("FPS: {}", get_fps()), screen_width()*0.01 + 40., 80.0, 20.0, BLACK); + draw_text(&format!("Array reads: {}", self.reads), screen_width()*0.01 + 40., 110.0, 20.0, BLACK); + draw_text(&format!("Array writes: {}", self.writes), screen_width()*0.01 + 40., 140.0, 20.0, BLACK); + draw_text(&format!("Comparisons: {}", self.comps), screen_width()*0.01 + 40., 170.0, 20.0, BLACK); + + }); if root_ui().button(Vec2::new(screen_width()*0.01, 60.), "Exit"){ diff --git a/src/algorithm.rs b/src/algorithm.rs index a17cc46..13d59f8 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -5,6 +5,7 @@ mod binaryHeap; mod coctailShaker; mod quickSort; mod bogoSort; +mod radixSortLSD; use crate::GuiHookVec::GuiVec; @@ -26,7 +27,8 @@ impl Algorithm{ "cocktailShaker".to_string(), "binaryHeap".to_string(), "quickSort".to_string(), - "radixSort".to_string(), + "radixSortMSD".to_string(), + "radixSortLSD".to_string() ] } } pub async fn run(length:usize, delay:f32, functionName:String){ @@ -41,7 +43,8 @@ impl Algorithm{ "cocktailShaker" => coctailShaker::cocktailShaker(&mut list).await, "binaryHeap" => binaryHeap::binaryHeap(&mut list).await, "quickSort" => quickSort::quickSort(&mut list).await, - "radixSort" => radixSort::radixSort(&mut list).await, + "radixSortMSD" => radixSort::radixSort(&mut list).await, + "radixSortLSD" => radixSortLSD::radixSort(&mut list).await, _ => panic!("No algorithm with that name implemented!") } diff --git a/src/algorithm/radixSortLSD.rs b/src/algorithm/radixSortLSD.rs new file mode 100644 index 0000000..f38bd59 --- /dev/null +++ b/src/algorithm/radixSortLSD.rs @@ -0,0 +1,94 @@ + + +use crate::GuiHookVec::SortingList; + + + +pub async fn radixSort(list:&mut impl SortingList) { + + let mut max = usize::MIN; + for i in list.getListClone().into_iter().map(|x| format!("{:b}",x.position)){ + + if max < i.len(){ + max = i.len(); + } + + } + for i in 0..(max){ + if radix(list, i).await {return}; + } +} + +pub async fn radix(list:&mut impl SortingList, radix:usize) -> bool{ + let mut bucket = vec![vec![];2]; + + for (i, bar) in list.elements().enumerate(){ + + let cur = if get_bit_at(bar.position, radix) {1} else {0}; + + bucket[cur].push(i); + + } + + + let mut sortedIndexes = Vec::new(); + for elements in bucket.into_iter(){ + for i in elements{ + sortedIndexes.push(i); + } + } + + let mut listClone = list.getListClone(); + let mut count = 0; + for i in sortedIndexes.clone(){ + if list.set(count, listClone[i]).await {return true}; + count += 1; + + } + + false +} + +fn get_bit_at(input: usize, n: usize) -> bool { + + if format!("{:b}", input).len() <= n{ + false + }else{ + input & (1 << n) != 0 + } + + +} +#[cfg(test)] +#[allow(non_snake_case)] +mod tests { + use crate::GuiHookVec::NonGuiVec; + +use super::*; + + + macro_rules! aw { + ($e:expr) => { + tokio_test::block_on($e) + }; + } + + #[test] + fn radixsort_correct() { + let mut list:NonGuiVec = SortingList::new(1000,0.0); + aw!(radixSort(&mut list)); + assert_eq!( list.isSorted(), true); + } + + #[test] + fn get_bit_at_test(){ + let num = 0b0001; + let othernum = 2; + assert_eq!(get_bit_at(num, 0), true); + assert_eq!(get_bit_at(num, 1), false); + assert_eq!(get_bit_at(othernum, 1), true); + assert_eq!(get_bit_at(othernum, 2), false); + assert_eq!(get_bit_at(othernum, 3), false); + } +} +