From 8e3665ed9559ad1ffe67746d333a1224e41d2c3a Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Mon, 8 Apr 2024 23:30:47 +0200 Subject: [PATCH] lab2 zad2 --- lab2/zad2/.gitignore | 2 + lab2/zad2/Cargo.lock | 87 ++++++++++++++++++++ lab2/zad2/Cargo.toml | 8 ++ lab2/zad2/gnuplot/k1/comp_small.gp | 12 +++ lab2/zad2/gnuplot/k1/swap_small.gp | 12 +++ lab2/zad2/src/main.rs | 125 +++++++++++++++++++++++++++++ 6 files changed, 246 insertions(+) create mode 100644 lab2/zad2/.gitignore create mode 100644 lab2/zad2/Cargo.lock create mode 100644 lab2/zad2/Cargo.toml create mode 100644 lab2/zad2/gnuplot/k1/comp_small.gp create mode 100644 lab2/zad2/gnuplot/k1/swap_small.gp create mode 100644 lab2/zad2/src/main.rs diff --git a/lab2/zad2/.gitignore b/lab2/zad2/.gitignore new file mode 100644 index 0000000..5a28a3d --- /dev/null +++ b/lab2/zad2/.gitignore @@ -0,0 +1,2 @@ +results/ +target/ diff --git a/lab2/zad2/Cargo.lock b/lab2/zad2/Cargo.lock new file mode 100644 index 0000000..1552d7d --- /dev/null +++ b/lab2/zad2/Cargo.lock @@ -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", +] diff --git a/lab2/zad2/Cargo.toml b/lab2/zad2/Cargo.toml new file mode 100644 index 0000000..a7c2510 --- /dev/null +++ b/lab2/zad2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "zad2" +version = "0.1.0" +edition = "2021" + +[dependencies] +libsort = { path = "../zad1/libsort" } +libgen = { path = "../zad1/libgen" } diff --git a/lab2/zad2/gnuplot/k1/comp_small.gp b/lab2/zad2/gnuplot/k1/comp_small.gp new file mode 100644 index 0000000..0779635 --- /dev/null +++ b/lab2/zad2/gnuplot/k1/comp_small.gp @@ -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 diff --git a/lab2/zad2/gnuplot/k1/swap_small.gp b/lab2/zad2/gnuplot/k1/swap_small.gp new file mode 100644 index 0000000..fd6d6cc --- /dev/null +++ b/lab2/zad2/gnuplot/k1/swap_small.gp @@ -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 diff --git a/lab2/zad2/src/main.rs b/lab2/zad2/src/main.rs new file mode 100644 index 0000000..1b0628b --- /dev/null +++ b/lab2/zad2/src/main.rs @@ -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>) -> HashMap { + results_map.iter() + .map(|(i, results)| + (*i, (results.iter() + .map(|res| res.comparisons) + .sum::() as f64 / results.len() as f64))) + .collect() +} + +fn swap_avg(results_map: &HashMap>) -> HashMap { + results_map.iter() + .map(|(i, results)| + (*i, (results.iter() + .map(|res| res.swaps) + .sum::() as f64 / results.len() as f64))) + .collect() +} + +fn main() -> io::Result<()> { + let k = args().nth(1) + .expect(format!("usage: {} ", args().nth(0).unwrap()).as_str()) + .parse::() + .expect("k must be u64"); + + let mut insertion_results_small: HashMap> = HashMap::new(); + let mut quick_results_small: HashMap> = HashMap::new(); + let mut hybrid_results_small: HashMap> = 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> = HashMap::new(); + let mut hybrid_results_large: HashMap> = 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(()) +}