lab2 zad1
This commit is contained in:
parent
fe27e7165c
commit
059c5efa6e
22 changed files with 704 additions and 0 deletions
1
lab2/zad1/.gitignore
vendored
Normal file
1
lab2/zad1/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*/target/
|
75
lab2/zad1/gen_asc/Cargo.lock
generated
Normal file
75
lab2/zad1/gen_asc/Cargo.lock
generated
Normal file
|
@ -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"
|
7
lab2/zad1/gen_asc/Cargo.toml
Normal file
7
lab2/zad1/gen_asc/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "gen_asc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
23
lab2/zad1/gen_asc/src/main.rs
Normal file
23
lab2/zad1/gen_asc/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
use std::{env::args, io};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
|
||||||
|
let amount = args().nth(1)
|
||||||
|
.expect(format!("usage: {} <amount>", args().nth(0).unwrap()).as_str())
|
||||||
|
.parse::<u64>()
|
||||||
|
.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(())
|
||||||
|
}
|
75
lab2/zad1/gen_desc/Cargo.lock
generated
Normal file
75
lab2/zad1/gen_desc/Cargo.lock
generated
Normal file
|
@ -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"
|
7
lab2/zad1/gen_desc/Cargo.toml
Normal file
7
lab2/zad1/gen_desc/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "gen_desc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
24
lab2/zad1/gen_desc/src/main.rs
Normal file
24
lab2/zad1/gen_desc/src/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use std::{env::args, io};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
|
||||||
|
let amount = args().nth(1)
|
||||||
|
.expect(format!("usage: {} <amount>", args().nth(0).unwrap()).as_str())
|
||||||
|
.parse::<u64>()
|
||||||
|
.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(())
|
||||||
|
}
|
75
lab2/zad1/gen_rand/Cargo.lock
generated
Normal file
75
lab2/zad1/gen_rand/Cargo.lock
generated
Normal file
|
@ -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"
|
7
lab2/zad1/gen_rand/Cargo.toml
Normal file
7
lab2/zad1/gen_rand/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "gen_rand"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
19
lab2/zad1/gen_rand/src/main.rs
Normal file
19
lab2/zad1/gen_rand/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
use std::{env::args, io};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
|
||||||
|
let amount = args().nth(1)
|
||||||
|
.expect(format!("usage: {} <amount>", args().nth(0).unwrap()).as_str())
|
||||||
|
.parse::<u64>()
|
||||||
|
.expect("amount must be u64");
|
||||||
|
|
||||||
|
println!("{}", amount);
|
||||||
|
|
||||||
|
for _ in 1..=amount {
|
||||||
|
println!("{}", rand::thread_rng().gen_range(0..(2 * amount)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
14
lab2/zad1/hybridsort/Cargo.lock
generated
Normal file
14
lab2/zad1/hybridsort/Cargo.lock
generated
Normal file
|
@ -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"
|
7
lab2/zad1/hybridsort/Cargo.toml
Normal file
7
lab2/zad1/hybridsort/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "hybridsort"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libsort = { path = "../libsort" }
|
53
lab2/zad1/hybridsort/src/main.rs
Normal file
53
lab2/zad1/hybridsort/src/main.rs
Normal file
|
@ -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::<u64>().expect("array length has to be u64");
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
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() < 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(())
|
||||||
|
}
|
14
lab2/zad1/insertionsort/Cargo.lock
generated
Normal file
14
lab2/zad1/insertionsort/Cargo.lock
generated
Normal file
|
@ -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"
|
7
lab2/zad1/insertionsort/Cargo.toml
Normal file
7
lab2/zad1/insertionsort/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "insertionsort"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libsort = { path = "../libsort" }
|
53
lab2/zad1/insertionsort/src/main.rs
Normal file
53
lab2/zad1/insertionsort/src/main.rs
Normal file
|
@ -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::<u64>().expect("array length has to be u64");
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
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() < 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(())
|
||||||
|
}
|
7
lab2/zad1/libsort/Cargo.lock
generated
Normal file
7
lab2/zad1/libsort/Cargo.lock
generated
Normal file
|
@ -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"
|
6
lab2/zad1/libsort/Cargo.toml
Normal file
6
lab2/zad1/libsort/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "libsort"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
156
lab2/zad1/libsort/src/lib.rs
Normal file
156
lab2/zad1/libsort/src/lib.rs
Normal file
|
@ -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!("<hybrid_insertion>"); }
|
||||||
|
let insertion_res = insertion_sort(list, print);
|
||||||
|
if print { println!("</hybrid_insertion>"); }
|
||||||
|
return insertion_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pivot = lomuto_partition(list, &mut res);
|
||||||
|
|
||||||
|
if print {
|
||||||
|
println!("<hybrid_quick>");
|
||||||
|
println!("pivot = {pivot}:");
|
||||||
|
print_list(&list);
|
||||||
|
println!("</hybrid_quick>");
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
14
lab2/zad1/quicksort/Cargo.lock
generated
Normal file
14
lab2/zad1/quicksort/Cargo.lock
generated
Normal file
|
@ -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",
|
||||||
|
]
|
7
lab2/zad1/quicksort/Cargo.toml
Normal file
7
lab2/zad1/quicksort/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "quicksort"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libsort = { path = "../libsort" }
|
53
lab2/zad1/quicksort/src/main.rs
Normal file
53
lab2/zad1/quicksort/src/main.rs
Normal file
|
@ -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::<u64>().expect("array length has to be u64");
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
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() < 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(())
|
||||||
|
}
|
Loading…
Reference in a new issue