From f450688cfba6886580bd802172bf3e53bd53977d Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Mon, 6 May 2024 23:00:03 +0200 Subject: [PATCH] more cleanup and rand_select impl --- lab3/rand_select/Cargo.lock | 88 ++++++++++++++++++++++++++++++ lab3/rand_select/Cargo.toml | 2 + lab3/rand_select/src/main.rs | 60 +++++++++++++++++++- libselect/src/randomized_select.rs | 16 +----- 4 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 lab3/rand_select/Cargo.lock diff --git a/lab3/rand_select/Cargo.lock b/lab3/rand_select/Cargo.lock new file mode 100644 index 0000000..382fae8 --- /dev/null +++ b/lab3/rand_select/Cargo.lock @@ -0,0 +1,88 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" + +[[package]] +name = "libselect" +version = "0.1.0" +dependencies = [ + "libsort", + "rand", +] + +[[package]] +name = "libsort" +version = "0.1.0" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_select" +version = "0.1.0" +dependencies = [ + "libselect", + "libsort", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/lab3/rand_select/Cargo.toml b/lab3/rand_select/Cargo.toml index 26c3f9d..1f5ebc6 100644 --- a/lab3/rand_select/Cargo.toml +++ b/lab3/rand_select/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +libselect = { path = "../../libselect" } +libsort = { path = "../../libsort" } diff --git a/lab3/rand_select/src/main.rs b/lab3/rand_select/src/main.rs index e7a11a9..2ed99e7 100644 --- a/lab3/rand_select/src/main.rs +++ b/lab3/rand_select/src/main.rs @@ -1,3 +1,59 @@ -fn main() { - println!("Hello, world!"); +use std::{io, process::exit}; + +use libselect::{randomized_select::RandomizedSelect, Select}; +use libsort::{insertion_sort::InsertionSort, Sort}; + +fn main() -> io::Result<()> { + let mut buffer = String::new(); + + let stdin = io::stdin(); + + stdin.read_line(&mut buffer)?; + + let list_len = buffer.trim().parse::().expect("array length has to be u64"); + buffer.clear(); + + stdin.read_line(&mut buffer)?; + + let order_statistic = buffer.trim().parse::().expect("order statistic has to be usize"); + buffer.clear(); + + if !(1..=list_len as usize).contains(&order_statistic) { + eprintln!("order statistic must be in range [1..{}]", list_len); + exit(1); + } + + let mut list: Vec = vec![]; + + for i in 0..list_len { + stdin.read_line(&mut buffer)?; + list.push( + buffer.trim().parse::() + .expect(format!("element {} has to be u64", i).as_str()) + ); + buffer.clear(); + } + + let input_list: Vec = list.clone(); + + let print = input_list.len() <= 50; + + let mut randomized = RandomizedSelect::new(print); + + let result = randomized.select_mut(&mut list, order_statistic); + + if print { + println!(); + println!("start state: {:?}", input_list); + println!("end state: {:?}", list); + let mut insertion = InsertionSort::new(false); + println!("sorted array: {:?}", insertion.sort(&input_list)); + println!("{} order statistic: {}", order_statistic, result); + } + + println!("swaps: {}", randomized.num_swap()); + println!("comparisons: {}", randomized.num_comp()); + + Ok(()) } + diff --git a/libselect/src/randomized_select.rs b/libselect/src/randomized_select.rs index d1dfc4d..3c032c8 100644 --- a/libselect/src/randomized_select.rs +++ b/libselect/src/randomized_select.rs @@ -1,6 +1,6 @@ use rand::{thread_rng, Rng}; -use crate::{normal_select::NormalSelect, CompareResult, Select}; +use crate::{normal_select::NormalSelect, Select}; pub struct RandomizedSelect { comparisons: u64, @@ -10,20 +10,6 @@ pub struct RandomizedSelect { } impl RandomizedSelect { - fn swap(&mut self, list: &mut Vec, i: usize, j: usize) { - self.swaps += 1; - list.swap(i, j); - } - fn compare(&mut self, a: u64, b: u64) -> CompareResult { - self.comparisons += 1; - if a < b { - CompareResult::LESS - } else if a > b { - CompareResult::GREATER - } else { - CompareResult::EQUAL - } - } fn rand_partition(&mut self, list: &mut Vec, lo: usize, hi: usize) -> usize { let pivot = thread_rng().gen_range(lo..hi);