lab2 zad2
This commit is contained in:
parent
7efd7621d1
commit
8e3665ed95
6 changed files with 246 additions and 0 deletions
2
lab2/zad2/.gitignore
vendored
Normal file
2
lab2/zad2/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
results/
|
||||
target/
|
87
lab2/zad2/Cargo.lock
generated
Normal file
87
lab2/zad2/Cargo.lock
generated
Normal file
|
@ -0,0 +1,87 @@
|
|||
# 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.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.153"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||
|
||||
[[package]]
|
||||
name = "libgen"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"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 = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "zad2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libgen",
|
||||
"libsort",
|
||||
]
|
8
lab2/zad2/Cargo.toml
Normal file
8
lab2/zad2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "zad2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libsort = { path = "../zad1/libsort" }
|
||||
libgen = { path = "../zad1/libgen" }
|
12
lab2/zad2/gnuplot/k1/comp_small.gp
Normal file
12
lab2/zad2/gnuplot/k1/comp_small.gp
Normal file
|
@ -0,0 +1,12 @@
|
|||
set title "Average comparisons, k = 1, n in {10, 20, ..., 50}"
|
||||
set style data lines
|
||||
|
||||
set xlabel "n"
|
||||
set ylabel "Average comparisons"
|
||||
|
||||
plot "./results/k1/small" \
|
||||
using 1:2 t "InsertionSort", \
|
||||
"" using 1:4 t "QuickSort", \
|
||||
"" using 1:6 t "HybridSort"
|
||||
|
||||
# vim: ft=gnuplot
|
12
lab2/zad2/gnuplot/k1/swap_small.gp
Normal file
12
lab2/zad2/gnuplot/k1/swap_small.gp
Normal file
|
@ -0,0 +1,12 @@
|
|||
set title "Average swaps, k = 1, n = {10, 20, ..., 50}"
|
||||
set style data lines
|
||||
|
||||
set xlabel "n"
|
||||
set ylabel "Average swaps"
|
||||
|
||||
plot "./results/k1/small" \
|
||||
using 1:3 t "InsertionSort", \
|
||||
"" using 1:5 t "QuickSort", \
|
||||
"" using 1:7 t "HybridSort"
|
||||
|
||||
# vim: ft=gnuplot
|
125
lab2/zad2/src/main.rs
Normal file
125
lab2/zad2/src/main.rs
Normal file
|
@ -0,0 +1,125 @@
|
|||
use std::{collections::HashMap, env::args, fs::{self, OpenOptions}};
|
||||
use std::io::{self, Write};
|
||||
|
||||
use libgen::gen_rand;
|
||||
use libsort::{hybrid_sort, insertion_sort, quick_sort, SortResult};
|
||||
|
||||
fn comp_avg(results_map: &HashMap<u64, Vec<SortResult>>) -> HashMap<u64, f64> {
|
||||
results_map.iter()
|
||||
.map(|(i, results)|
|
||||
(*i, (results.iter()
|
||||
.map(|res| res.comparisons)
|
||||
.sum::<u64>() as f64 / results.len() as f64)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn swap_avg(results_map: &HashMap<u64, Vec<SortResult>>) -> HashMap<u64, f64> {
|
||||
results_map.iter()
|
||||
.map(|(i, results)|
|
||||
(*i, (results.iter()
|
||||
.map(|res| res.swaps)
|
||||
.sum::<u64>() as f64 / results.len() as f64)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let k = args().nth(1)
|
||||
.expect(format!("usage: {} <k>", args().nth(0).unwrap()).as_str())
|
||||
.parse::<u64>()
|
||||
.expect("k must be u64");
|
||||
|
||||
let mut insertion_results_small: HashMap<u64, Vec<SortResult>> = HashMap::new();
|
||||
let mut quick_results_small: HashMap<u64, Vec<SortResult>> = HashMap::new();
|
||||
let mut hybrid_results_small: HashMap<u64, Vec<SortResult>> = HashMap::new();
|
||||
|
||||
for n in (10..=50).step_by(10) {
|
||||
insertion_results_small.insert(n, vec![]);
|
||||
quick_results_small.insert(n, vec![]);
|
||||
hybrid_results_small.insert(n, vec![]);
|
||||
for _ in 0..k {
|
||||
insertion_results_small.get_mut(&n).unwrap().push(insertion_sort(&mut gen_rand(n), false));
|
||||
quick_results_small.get_mut(&n).unwrap().push(quick_sort(&mut gen_rand(n), false));
|
||||
hybrid_results_small.get_mut(&n).unwrap().push(hybrid_sort(&mut gen_rand(n), 8, false));
|
||||
}
|
||||
}
|
||||
|
||||
let mut quick_results_large: HashMap<u64, Vec<SortResult>> = HashMap::new();
|
||||
let mut hybrid_results_large: HashMap<u64, Vec<SortResult>> = HashMap::new();
|
||||
|
||||
for n in (1000..=50000).step_by(1000) {
|
||||
quick_results_large.insert(n, vec![]);
|
||||
hybrid_results_large.insert(n, vec![]);
|
||||
for _ in 0..k {
|
||||
quick_results_large.get_mut(&n).unwrap().push(quick_sort(&mut gen_rand(n), false));
|
||||
hybrid_results_large.get_mut(&n).unwrap().push(hybrid_sort(&mut gen_rand(n), 8, false));
|
||||
}
|
||||
}
|
||||
|
||||
let insertion_comp_averages_small = comp_avg(&insertion_results_small);
|
||||
|
||||
let insertion_swap_averages_small = swap_avg(&insertion_results_small);
|
||||
|
||||
let quick_comp_averages_small = comp_avg(&quick_results_small);
|
||||
|
||||
let quick_swap_averages_small = swap_avg(&quick_results_small);
|
||||
|
||||
let hybrid_comp_averages_small = comp_avg(&hybrid_results_small);
|
||||
|
||||
let hybrid_swap_averages_small = swap_avg(&hybrid_results_small);
|
||||
|
||||
let quick_comp_averages_large = comp_avg(&quick_results_large);
|
||||
|
||||
let quick_swap_averages_large = swap_avg(&quick_results_large);
|
||||
|
||||
let hybrid_comp_averages_large = comp_avg(&hybrid_results_large);
|
||||
|
||||
let hybrid_swap_averages_large = swap_avg(&hybrid_results_large);
|
||||
|
||||
_ = fs::create_dir_all(format!("./results/k{k}"));
|
||||
|
||||
_ = fs::remove_file(format!("./results/k{k}/small"));
|
||||
|
||||
let mut small = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(format!("./results/k{k}/small"))?;
|
||||
|
||||
for n in (10..=50).step_by(10) {
|
||||
writeln!(small, "{n} {} {} {} {} {} {} {} {} {} {} {} {}",
|
||||
insertion_comp_averages_small[&n],
|
||||
insertion_swap_averages_small[&n],
|
||||
quick_comp_averages_small[&n],
|
||||
quick_swap_averages_small[&n],
|
||||
hybrid_comp_averages_small[&n],
|
||||
hybrid_swap_averages_small[&n],
|
||||
insertion_comp_averages_small[&n] / n as f64,
|
||||
insertion_swap_averages_small[&n] / n as f64,
|
||||
quick_comp_averages_small[&n] / n as f64,
|
||||
quick_swap_averages_small[&n] / n as f64,
|
||||
hybrid_comp_averages_small[&n] / n as f64,
|
||||
hybrid_swap_averages_small[&n] / n as f64,
|
||||
)?;
|
||||
}
|
||||
|
||||
_ = fs::remove_file(format!("./results/k{k}/large"));
|
||||
|
||||
let mut large = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(format!("./results/k{k}/large"))?;
|
||||
|
||||
for n in (1000..=50000).step_by(1000) {
|
||||
writeln!(large, "{n} {} {} {} {} {} {} {} {}",
|
||||
quick_comp_averages_large[&n],
|
||||
quick_swap_averages_large[&n],
|
||||
hybrid_comp_averages_large[&n],
|
||||
hybrid_swap_averages_large[&n],
|
||||
quick_comp_averages_large[&n] / n as f64,
|
||||
quick_swap_averages_large[&n] / n as f64,
|
||||
hybrid_comp_averages_large[&n] / n as f64,
|
||||
hybrid_swap_averages_large[&n] / n as f64,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue