debugged radix sort
This commit is contained in:
parent
37c7f922ce
commit
5b33b2d651
3 changed files with 169 additions and 54 deletions
|
|
@ -1,24 +1,32 @@
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Queue<T> {
|
pub struct Queue<T> {
|
||||||
queue: Vec<T>,
|
queue: VecDeque<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Queue<T> {
|
impl<T> Queue<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Queue { queue: Vec::new() }
|
Queue { queue: VecDeque::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn length(&self) -> usize {
|
pub fn length(&self) -> usize {
|
||||||
self.queue.len()
|
self.queue.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enqueue(&mut self, item: T) {
|
pub fn enqueue(&mut self, item: T) {
|
||||||
self.queue.push(item)
|
self.queue.push_back(item);
|
||||||
}
|
}
|
||||||
pub fn dequeue(&mut self) -> T {
|
|
||||||
self.queue.remove(0)
|
pub fn dequeue(&mut self) -> Option<T> {
|
||||||
|
self.queue.pop_front()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.queue.is_empty()
|
self.queue.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn peek(&self) -> Option<&T> {
|
pub fn peek(&self) -> Option<&T> {
|
||||||
self.queue.first()
|
self.queue.front()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
src/main.rs
16
src/main.rs
|
|
@ -71,14 +71,14 @@ fn merge_sort_demo(){
|
||||||
println!("Array before merge sort: {:?}",arr);
|
println!("Array before merge sort: {:?}",arr);
|
||||||
println!("Array after merge sort: {:?}",merge_sort::merge_sort(arr));
|
println!("Array after merge sort: {:?}",merge_sort::merge_sort(arr));
|
||||||
}
|
}
|
||||||
// fn radix_sort_demo(){
|
fn radix_sort_demo(){
|
||||||
// println!("-------------------");
|
println!("-------------------");
|
||||||
// println!("RADIX SORT DEMO:");
|
println!("RADIX SORT DEMO:");
|
||||||
// println!("-------------------");
|
println!("-------------------");
|
||||||
// let arr = &[9, 3, 7, 4, 69, 420, 42];
|
let arr = vec![9, 3, 7, 4, 69, 420, 42];
|
||||||
// println!("Array before radix sort: {:?}",arr);
|
println!("Array before radix sort: {:?}",arr);
|
||||||
// println!("Array after radix sort: {:?}",radix_sort::radix_sort(arr));
|
println!("Array after radix sort: {:?}",radix_sort::radix_sort(arr));
|
||||||
// }
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("");
|
println!("");
|
||||||
println!("|~~~~~~~~~~~~~~~|");
|
println!("|~~~~~~~~~~~~~~~|");
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,153 @@
|
||||||
// use crate::data_structures::stack::Stack;
|
use crate::data_structures::queue::Queue;
|
||||||
// pub fn radix_sort(arr: &[u64]) -> Vec<u64> {
|
/// Represents a bucket containing a value and a queue of unsigned 64-bit integers.
|
||||||
// let mut buckets: [Stack<u64>; 10] = [
|
#[derive(Debug)]
|
||||||
// Stack::new(),
|
struct Bucket {
|
||||||
// Stack::new(),
|
value: u8,
|
||||||
// Stack::new(),
|
queue: Queue<u64>,
|
||||||
// Stack::new(),
|
}
|
||||||
// Stack::new(),
|
|
||||||
// Stack::new(),
|
|
||||||
// Stack::new(),
|
|
||||||
// Stack::new(),
|
|
||||||
// Stack::new(),
|
|
||||||
// Stack::new(),
|
|
||||||
// ];
|
|
||||||
// let mut sort_vec = arr.to_vec();
|
|
||||||
// let largest_place_value = get_largest_place(arr);
|
|
||||||
|
|
||||||
// for counter in 0..largest_place_value {
|
impl Bucket {
|
||||||
// for num in &sort_vec {
|
/// Creates a new Bucket instance with initial value 0 and an empty queue.
|
||||||
// let digit = get_digit(*num, counter as usize);
|
fn new() -> Self {
|
||||||
// buckets[digit].push(*num);
|
Bucket {
|
||||||
// }
|
value: 0,
|
||||||
|
queue: Queue::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// let mut index = 0;
|
/// Represents a collection of buckets.
|
||||||
// for stack in &mut buckets {
|
#[derive(Debug)]
|
||||||
// while let Some(value) = stack.pop() {
|
struct Buckets {
|
||||||
// sort_vec[index] = value;
|
buckets: Vec<Bucket>,
|
||||||
// index += 1;
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sort_vec
|
impl Buckets {
|
||||||
// }
|
/// Creates a new Buckets instance with 10 empty buckets.
|
||||||
|
fn new() -> Self {
|
||||||
|
let mut buckets = Vec::with_capacity(10);
|
||||||
|
for i in 0..10 {
|
||||||
|
buckets.push(Bucket {value: i as u8, queue: Queue::new()});
|
||||||
|
}
|
||||||
|
Buckets { buckets }
|
||||||
|
}
|
||||||
|
|
||||||
// fn get_digit(num: u64, place: usize) -> usize {
|
/// Enqueues an item into the bucket at the specified index.
|
||||||
// (num / 10u64.pow(place as u32)) as usize % 10
|
///
|
||||||
// }
|
/// # Panics
|
||||||
|
/// Panics if the specified bucket index is out of range.
|
||||||
|
fn enqueue(&mut self, bucket_index: usize, item: u64) {
|
||||||
|
if let Some(bucket) = self.buckets.get_mut(bucket_index) {
|
||||||
|
bucket.queue.enqueue(item);
|
||||||
|
} else {
|
||||||
|
panic!("Invalid bucket index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dequeues an item from the bucket at the specified index.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// Panics if the specified bucket index is out of range.
|
||||||
|
fn dequeue(&mut self, bucket_index: usize) -> Option<u64> {
|
||||||
|
if let Some(bucket) = self.buckets.get_mut(bucket_index) {
|
||||||
|
bucket.queue.dequeue()
|
||||||
|
} else {
|
||||||
|
panic!("Invalid bucket index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the bucket at the specified index is empty.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// Panics if the specified bucket index is out of range.
|
||||||
|
fn is_empty(&self, bucket_index: usize) -> bool {
|
||||||
|
if let Some(bucket) = self.buckets.get(bucket_index) {
|
||||||
|
bucket.queue.is_empty()
|
||||||
|
} else {
|
||||||
|
panic!("Invalid bucket index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves a reference to the front item of the bucket at the specified index.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// Panics if the specified bucket index is out of range.
|
||||||
|
fn peek(&self, bucket_index: usize) -> Option<&u64> {
|
||||||
|
if let Some(bucket) = self.buckets.get(bucket_index) {
|
||||||
|
bucket.queue.peek()
|
||||||
|
} else {
|
||||||
|
panic!("Invalid bucket index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sorts a vector of unsigned 64-bit integers using radix sort algorithm.
|
||||||
|
pub fn radix_sort(arr: Vec<u64>) -> Vec<u64>{
|
||||||
|
let largest_unit = get_largest_place(arr.clone());
|
||||||
|
let mut counter = 0;
|
||||||
|
let mut buckets = Buckets::new();
|
||||||
|
let mut sort_vec: Vec<u64> = arr.clone();
|
||||||
|
while counter <= largest_unit {
|
||||||
|
for i in sort_vec.clone() {
|
||||||
|
let sort_value = get_value_at_unit(i, counter);
|
||||||
|
match sort_value {
|
||||||
|
0 => buckets.enqueue(0, i),
|
||||||
|
1 => buckets.enqueue(1, i),
|
||||||
|
2 => buckets.enqueue(2, i),
|
||||||
|
3 => buckets.enqueue(3, i),
|
||||||
|
4 => buckets.enqueue(4, i),
|
||||||
|
5 => buckets.enqueue(5, i),
|
||||||
|
6 => buckets.enqueue(6, i),
|
||||||
|
7 => buckets.enqueue(7, i),
|
||||||
|
8 => buckets.enqueue(8, i),
|
||||||
|
9 => buckets.enqueue(9, i),
|
||||||
|
_ => buckets.enqueue(0, i),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sort_vec.clear();
|
||||||
|
for j in 0..buckets.buckets.len() {
|
||||||
|
while let Some(value) = buckets.dequeue(j) {
|
||||||
|
sort_vec.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
return sort_vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// fn get_largest_place(arr: &[u64]) -> usize {
|
/// Retrieves the digit at the specified place from a given number.
|
||||||
// arr.iter()
|
fn get_value_at_unit(num: u64, place: u64) -> u64 {
|
||||||
// .map(|&num| (num as f64).log(10.0).ceil() as usize)
|
(num / 10u64.pow(place as u32)) % 10
|
||||||
// .max()
|
}
|
||||||
// .unwrap_or(0)
|
|
||||||
// }
|
/// Calculates the largest place value among a vector of unsigned 64-bit integers.
|
||||||
|
fn get_largest_place(arr: Vec<u64>) -> u64 {
|
||||||
|
arr.iter().map(|&num| (num as f64).log(10.0).ceil() as u64).max().unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validates if a vector of unsigned 64-bit integers is sorted in ascending order.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `arr` - A vector of unsigned 64-bit integers.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Result<(), &'static str>` - `Ok(())` if the vector is sorted, otherwise `Err("The vector is not sorted")`.
|
||||||
|
fn validate_sort(arr: Vec<u64>) -> Result<(), &'static str> {
|
||||||
|
// Check if the vector is empty
|
||||||
|
if arr.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through the vector and check if each element is less than or equal to the next one
|
||||||
|
for i in 0..arr.len() - 1 {
|
||||||
|
if arr[i] > arr[i + 1] {
|
||||||
|
return Err("The vector is not sorted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all elements are in sorted order, return Ok(())
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue