more cleanup and rand_select impl
This commit is contained in:
parent
d0de63ca35
commit
f450688cfb
4 changed files with 149 additions and 17 deletions
88
lab3/rand_select/Cargo.lock
generated
Normal file
88
lab3/rand_select/Cargo.lock
generated
Normal file
|
@ -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"
|
|
@ -4,3 +4,5 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
libselect = { path = "../../libselect" }
|
||||||
|
libsort = { path = "../../libsort" }
|
||||||
|
|
|
@ -1,3 +1,59 @@
|
||||||
fn main() {
|
use std::{io, process::exit};
|
||||||
println!("Hello, world!");
|
|
||||||
|
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::<u64>().expect("array length has to be u64");
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
stdin.read_line(&mut buffer)?;
|
||||||
|
|
||||||
|
let order_statistic = buffer.trim().parse::<usize>().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<u64> = vec![];
|
||||||
|
|
||||||
|
for i in 0..list_len {
|
||||||
|
stdin.read_line(&mut buffer)?;
|
||||||
|
list.push(
|
||||||
|
buffer.trim().parse::<u64>()
|
||||||
|
.expect(format!("element {} has to be u64", i).as_str())
|
||||||
|
);
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
let input_list: Vec<u64> = 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(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
use crate::{normal_select::NormalSelect, CompareResult, Select};
|
use crate::{normal_select::NormalSelect, Select};
|
||||||
|
|
||||||
pub struct RandomizedSelect {
|
pub struct RandomizedSelect {
|
||||||
comparisons: u64,
|
comparisons: u64,
|
||||||
|
@ -10,20 +10,6 @@ pub struct RandomizedSelect {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RandomizedSelect {
|
impl RandomizedSelect {
|
||||||
fn swap(&mut self, list: &mut Vec<u64>, 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<u64>, lo: usize, hi: usize) -> usize {
|
fn rand_partition(&mut self, list: &mut Vec<u64>, lo: usize, hi: usize) -> usize {
|
||||||
let pivot = thread_rng().gen_range(lo..hi);
|
let pivot = thread_rng().gen_range(lo..hi);
|
||||||
|
|
Loading…
Reference in a new issue