Added MSB and LSB radix sort algorithms
This commit is contained in:
parent
fb40910c10
commit
3e013192d0
3 changed files with 106 additions and 14 deletions
|
@ -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"){
|
||||
|
|
|
@ -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!")
|
||||
}
|
||||
|
||||
|
|
94
src/algorithm/radixSortLSD.rs
Normal file
94
src/algorithm/radixSortLSD.rs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue