From 059c5efa6e9230b82f9edfb9ca750528e1939b9c Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Mon, 8 Apr 2024 21:12:52 +0200 Subject: [PATCH] lab2 zad1 --- lab2/zad1/.gitignore | 1 + lab2/zad1/gen_asc/Cargo.lock | 75 +++++++++++++ lab2/zad1/gen_asc/Cargo.toml | 7 ++ lab2/zad1/gen_asc/src/main.rs | 23 ++++ lab2/zad1/gen_desc/Cargo.lock | 75 +++++++++++++ lab2/zad1/gen_desc/Cargo.toml | 7 ++ lab2/zad1/gen_desc/src/main.rs | 24 +++++ lab2/zad1/gen_rand/Cargo.lock | 75 +++++++++++++ lab2/zad1/gen_rand/Cargo.toml | 7 ++ lab2/zad1/gen_rand/src/main.rs | 19 ++++ lab2/zad1/hybridsort/Cargo.lock | 14 +++ lab2/zad1/hybridsort/Cargo.toml | 7 ++ lab2/zad1/hybridsort/src/main.rs | 53 ++++++++++ lab2/zad1/insertionsort/Cargo.lock | 14 +++ lab2/zad1/insertionsort/Cargo.toml | 7 ++ lab2/zad1/insertionsort/src/main.rs | 53 ++++++++++ lab2/zad1/libsort/Cargo.lock | 7 ++ lab2/zad1/libsort/Cargo.toml | 6 ++ lab2/zad1/libsort/src/lib.rs | 156 ++++++++++++++++++++++++++++ lab2/zad1/quicksort/Cargo.lock | 14 +++ lab2/zad1/quicksort/Cargo.toml | 7 ++ lab2/zad1/quicksort/src/main.rs | 53 ++++++++++ 22 files changed, 704 insertions(+) create mode 100644 lab2/zad1/.gitignore create mode 100644 lab2/zad1/gen_asc/Cargo.lock create mode 100644 lab2/zad1/gen_asc/Cargo.toml create mode 100644 lab2/zad1/gen_asc/src/main.rs create mode 100644 lab2/zad1/gen_desc/Cargo.lock create mode 100644 lab2/zad1/gen_desc/Cargo.toml create mode 100644 lab2/zad1/gen_desc/src/main.rs create mode 100644 lab2/zad1/gen_rand/Cargo.lock create mode 100644 lab2/zad1/gen_rand/Cargo.toml create mode 100644 lab2/zad1/gen_rand/src/main.rs create mode 100644 lab2/zad1/hybridsort/Cargo.lock create mode 100644 lab2/zad1/hybridsort/Cargo.toml create mode 100644 lab2/zad1/hybridsort/src/main.rs create mode 100644 lab2/zad1/insertionsort/Cargo.lock create mode 100644 lab2/zad1/insertionsort/Cargo.toml create mode 100644 lab2/zad1/insertionsort/src/main.rs create mode 100644 lab2/zad1/libsort/Cargo.lock create mode 100644 lab2/zad1/libsort/Cargo.toml create mode 100644 lab2/zad1/libsort/src/lib.rs create mode 100644 lab2/zad1/quicksort/Cargo.lock create mode 100644 lab2/zad1/quicksort/Cargo.toml create mode 100644 lab2/zad1/quicksort/src/main.rs diff --git a/lab2/zad1/.gitignore b/lab2/zad1/.gitignore new file mode 100644 index 0000000..901b6e6 --- /dev/null +++ b/lab2/zad1/.gitignore @@ -0,0 +1 @@ +*/target/ diff --git a/lab2/zad1/gen_asc/Cargo.lock b/lab2/zad1/gen_asc/Cargo.lock new file mode 100644 index 0000000..da56c50 --- /dev/null +++ b/lab2/zad1/gen_asc/Cargo.lock @@ -0,0 +1,75 @@ +# 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 = "gen_asc" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[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 = "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" diff --git a/lab2/zad1/gen_asc/Cargo.toml b/lab2/zad1/gen_asc/Cargo.toml new file mode 100644 index 0000000..aa353c7 --- /dev/null +++ b/lab2/zad1/gen_asc/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gen_asc" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/lab2/zad1/gen_asc/src/main.rs b/lab2/zad1/gen_asc/src/main.rs new file mode 100644 index 0000000..34a39ea --- /dev/null +++ b/lab2/zad1/gen_asc/src/main.rs @@ -0,0 +1,23 @@ +use std::{env::args, io}; + +use rand::Rng; + +fn main() -> io::Result<()> { + + let amount = args().nth(1) + .expect(format!("usage: {} ", args().nth(0).unwrap()).as_str()) + .parse::() + .expect("amount must be u64"); + + println!("{}", amount); + + let mut last = -1; + + for i in 1..=amount { + let current = rand::thread_rng().gen_range((last + 1)..=(2 * amount - (amount - i)) as i64); + println!("{}", current); + last = current; + } + + Ok(()) +} diff --git a/lab2/zad1/gen_desc/Cargo.lock b/lab2/zad1/gen_desc/Cargo.lock new file mode 100644 index 0000000..e4c0c2f --- /dev/null +++ b/lab2/zad1/gen_desc/Cargo.lock @@ -0,0 +1,75 @@ +# 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 = "gen_desc" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[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 = "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" diff --git a/lab2/zad1/gen_desc/Cargo.toml b/lab2/zad1/gen_desc/Cargo.toml new file mode 100644 index 0000000..d21d1a0 --- /dev/null +++ b/lab2/zad1/gen_desc/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gen_desc" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/lab2/zad1/gen_desc/src/main.rs b/lab2/zad1/gen_desc/src/main.rs new file mode 100644 index 0000000..40b987d --- /dev/null +++ b/lab2/zad1/gen_desc/src/main.rs @@ -0,0 +1,24 @@ +use std::{env::args, io}; + +use rand::Rng; + +fn main() -> io::Result<()> { + + let amount = args().nth(1) + .expect(format!("usage: {} ", args().nth(0).unwrap()).as_str()) + .parse::() + .expect("amount must be u64"); + + println!("{}", amount); + + let mut last = rand::thread_rng().gen_range((amount)..(2 * amount)); + println!("{}", last); + + for i in 1..amount { + let current = rand::thread_rng().gen_range((amount - i)..(last)); + println!("{}", current); + last = current; + } + + Ok(()) +} diff --git a/lab2/zad1/gen_rand/Cargo.lock b/lab2/zad1/gen_rand/Cargo.lock new file mode 100644 index 0000000..29d38de --- /dev/null +++ b/lab2/zad1/gen_rand/Cargo.lock @@ -0,0 +1,75 @@ +# 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 = "gen_rand" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[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 = "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" diff --git a/lab2/zad1/gen_rand/Cargo.toml b/lab2/zad1/gen_rand/Cargo.toml new file mode 100644 index 0000000..174aedd --- /dev/null +++ b/lab2/zad1/gen_rand/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gen_rand" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/lab2/zad1/gen_rand/src/main.rs b/lab2/zad1/gen_rand/src/main.rs new file mode 100644 index 0000000..ad762b6 --- /dev/null +++ b/lab2/zad1/gen_rand/src/main.rs @@ -0,0 +1,19 @@ +use std::{env::args, io}; + +use rand::Rng; + +fn main() -> io::Result<()> { + + let amount = args().nth(1) + .expect(format!("usage: {} ", args().nth(0).unwrap()).as_str()) + .parse::() + .expect("amount must be u64"); + + println!("{}", amount); + + for _ in 1..=amount { + println!("{}", rand::thread_rng().gen_range(0..(2 * amount))); + } + + Ok(()) +} diff --git a/lab2/zad1/hybridsort/Cargo.lock b/lab2/zad1/hybridsort/Cargo.lock new file mode 100644 index 0000000..b3090b6 --- /dev/null +++ b/lab2/zad1/hybridsort/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hybridsort" +version = "0.1.0" +dependencies = [ + "libsort", +] + +[[package]] +name = "libsort" +version = "0.1.0" diff --git a/lab2/zad1/hybridsort/Cargo.toml b/lab2/zad1/hybridsort/Cargo.toml new file mode 100644 index 0000000..02548ff --- /dev/null +++ b/lab2/zad1/hybridsort/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hybridsort" +version = "0.1.0" +edition = "2021" + +[dependencies] +libsort = { path = "../libsort" } diff --git a/lab2/zad1/hybridsort/src/main.rs b/lab2/zad1/hybridsort/src/main.rs new file mode 100644 index 0000000..7cb7fa5 --- /dev/null +++ b/lab2/zad1/hybridsort/src/main.rs @@ -0,0 +1,53 @@ +use std::io; + +use libsort::{hybrid_sort, is_sorted, print_list}; + +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(); + + 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() < 40; + + if print { + print!("input: "); + print_list(&input_list); + println!(); + } + + let res = hybrid_sort(&mut list, 8, print); + + if print { + println!(); + print!("input: "); + print_list(&input_list); + println!(); + print!("output: "); + print_list(&list); + } + + println!("swaps: {}", res.swaps); + println!("comparisons: {}", res.comparisons); + + println!("is sorted: {}", is_sorted(&list)); + + Ok(()) +} diff --git a/lab2/zad1/insertionsort/Cargo.lock b/lab2/zad1/insertionsort/Cargo.lock new file mode 100644 index 0000000..7a56b9a --- /dev/null +++ b/lab2/zad1/insertionsort/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "insertionsort" +version = "0.1.0" +dependencies = [ + "libsort", +] + +[[package]] +name = "libsort" +version = "0.1.0" diff --git a/lab2/zad1/insertionsort/Cargo.toml b/lab2/zad1/insertionsort/Cargo.toml new file mode 100644 index 0000000..ae4d971 --- /dev/null +++ b/lab2/zad1/insertionsort/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "insertionsort" +version = "0.1.0" +edition = "2021" + +[dependencies] +libsort = { path = "../libsort" } diff --git a/lab2/zad1/insertionsort/src/main.rs b/lab2/zad1/insertionsort/src/main.rs new file mode 100644 index 0000000..5524375 --- /dev/null +++ b/lab2/zad1/insertionsort/src/main.rs @@ -0,0 +1,53 @@ +use std::io; + +use libsort::{insertion_sort, is_sorted, print_list}; + +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(); + + 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() < 40; + + if print { + print!("input: "); + print_list(&input_list); + println!(); + } + + let res = insertion_sort(&mut list, print); + + if print { + println!(); + print!("input: "); + print_list(&input_list); + println!(); + print!("output: "); + print_list(&list); + } + + println!("swaps: {}", res.swaps); + println!("comparisons: {}", res.comparisons); + + println!("is sorted: {}", is_sorted(&list)); + + Ok(()) +} diff --git a/lab2/zad1/libsort/Cargo.lock b/lab2/zad1/libsort/Cargo.lock new file mode 100644 index 0000000..558dcc7 --- /dev/null +++ b/lab2/zad1/libsort/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libsort" +version = "0.1.0" diff --git a/lab2/zad1/libsort/Cargo.toml b/lab2/zad1/libsort/Cargo.toml new file mode 100644 index 0000000..772aafc --- /dev/null +++ b/lab2/zad1/libsort/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libsort" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/lab2/zad1/libsort/src/lib.rs b/lab2/zad1/libsort/src/lib.rs new file mode 100644 index 0000000..6e97f3a --- /dev/null +++ b/lab2/zad1/libsort/src/lib.rs @@ -0,0 +1,156 @@ +pub fn print_list(list: &[u64]) { + print!("["); + list.iter() + .enumerate() + .for_each(|(i, x)| { + if *x < 10 { + print!("0"); + } + print!("{}", x); + if i != list.len() - 1 { + print!(", ") + } + }); + println!("]"); +} + +#[derive(PartialEq)] +enum CompareResult { + LESS, EQUAL, GREATER +} + +fn compare(a: u64, b: u64, comparisons: &mut u64) -> CompareResult { + *comparisons += 1; + if a < b { + CompareResult::LESS + } else if a > b { + CompareResult::GREATER + } else { + CompareResult::EQUAL + } +} + +fn swap(list: &mut [u64], i: usize, j: usize, swaps: &mut u64) { + *swaps += 1; + list.swap(i, j); +} + +#[derive(Debug)] +pub struct SortResult { + pub comparisons: u64, + pub swaps: u64, +} + +impl Default for SortResult { + fn default() -> Self { + Self { comparisons: 0, swaps: 0 } + } +} + +use CompareResult::*; + +pub fn insertion_sort(list: &mut [u64], print: bool) -> SortResult { + let mut res = SortResult::default(); + for i in 1..list.len() { + let mut j = i as usize; + + while j > 0 && compare(list[j - 1], list[j], &mut res.comparisons) == GREATER { + swap(list, j - 1, j, &mut res.swaps); + j -= 1; + } + + if print { print_list(&list); } + } + + res +} + +pub fn lomuto_partition(list: &mut [u64], res: &mut SortResult) -> u64 { + let mut _res = SortResult::default(); + + let pivot = list.len() - 1; + let mut _swap = 0; + + for i in 0..pivot { + if compare(list[i as usize], list[pivot as usize], &mut _res.comparisons) == LESS { + if _swap != i { + swap(list, _swap, i, &mut _res.swaps); + } + _swap += 1; + } + } + + if _swap != pivot { + list.swap(_swap, pivot); + } + + res.swaps += _res.swaps; + res.comparisons += _res.comparisons; + + _swap as u64 +} + +pub fn quick_sort(list: &mut [u64], print: bool) -> SortResult { + let mut res = SortResult::default(); + + if list.len() > 1 { + println!("before partition: {:?}", res); + let pivot = lomuto_partition(list, &mut res); + println!("after partition: {:?}", res); + + if print { + println!("pivot = {pivot}:"); + print_list(&list); + } + + let res1 = quick_sort(&mut list[..pivot as usize], print); + res.swaps += res1.swaps; + res.comparisons += res1.comparisons; + + let res2 = quick_sort(&mut list[pivot as usize + 1..], print); + res.swaps += res2.swaps; + res.comparisons += res2.comparisons; + } + + res +} + +pub fn hybrid_sort(list: &mut [u64], switch: u64, print: bool) -> SortResult { + let mut res = SortResult::default(); + + if list.len() as u64 <= switch { + if print { println!(""); } + let insertion_res = insertion_sort(list, print); + if print { println!(""); } + return insertion_res; + } + + let pivot = lomuto_partition(list, &mut res); + + if print { + println!(""); + println!("pivot = {pivot}:"); + print_list(&list); + println!(""); + } + + let res1 = hybrid_sort(&mut list[..pivot as usize], switch, print); + res.swaps += res1.swaps; + res.comparisons += res1.comparisons; + + let res2 = hybrid_sort(&mut list[pivot as usize + 1..], switch, print); + res.swaps += res2.swaps; + res.comparisons += res2.comparisons; + + res +} + +pub fn is_sorted(list: &[u64]) -> bool { + for i in 0..(list.len() - 1) { + if list[i] > list[i + 1] { + return false; + } + } + + true +} diff --git a/lab2/zad1/quicksort/Cargo.lock b/lab2/zad1/quicksort/Cargo.lock new file mode 100644 index 0000000..2388221 --- /dev/null +++ b/lab2/zad1/quicksort/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libsort" +version = "0.1.0" + +[[package]] +name = "quicksort" +version = "0.1.0" +dependencies = [ + "libsort", +] diff --git a/lab2/zad1/quicksort/Cargo.toml b/lab2/zad1/quicksort/Cargo.toml new file mode 100644 index 0000000..9fb3c6e --- /dev/null +++ b/lab2/zad1/quicksort/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "quicksort" +version = "0.1.0" +edition = "2021" + +[dependencies] +libsort = { path = "../libsort" } diff --git a/lab2/zad1/quicksort/src/main.rs b/lab2/zad1/quicksort/src/main.rs new file mode 100644 index 0000000..8f7ce3f --- /dev/null +++ b/lab2/zad1/quicksort/src/main.rs @@ -0,0 +1,53 @@ +use std::io; + +use libsort::{is_sorted, print_list, quick_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(); + + 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() < 40; + + if print { + print!("input: "); + print_list(&input_list); + println!(); + } + + let res = quick_sort(&mut list, print); + + if print { + println!(); + print!("input: "); + print_list(&input_list); + println!(); + print!("output: "); + print_list(&list); + } + + println!("swaps: {}", res.swaps); + println!("comparisons: {}", res.comparisons); + + println!("is sorted: {}", is_sorted(&list)); + + Ok(()) +}