init lab3
This commit is contained in:
parent
118d4e911a
commit
ee93f577e1
17 changed files with 659 additions and 0 deletions
1
lab3/.gitignore
vendored
Normal file
1
lab3/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*/target
|
82
lab3/gen_asc/Cargo.lock
generated
Normal file
82
lab3/gen_asc/Cargo.lock
generated
Normal file
|
@ -0,0 +1,82 @@
|
|||
# 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 = [
|
||||
"libgen",
|
||||
]
|
||||
|
||||
[[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 = "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
lab3/gen_asc/Cargo.toml
Normal file
7
lab3/gen_asc/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "gen_asc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libgen = { path = "../libgen" }
|
19
lab3/gen_asc/src/main.rs
Normal file
19
lab3/gen_asc/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::{env::args, io};
|
||||
|
||||
use libgen::gen_asc;
|
||||
|
||||
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);
|
||||
|
||||
gen_asc(amount)
|
||||
.iter()
|
||||
.for_each(|x| println!("{}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
82
lab3/gen_desc/Cargo.lock
generated
Normal file
82
lab3/gen_desc/Cargo.lock
generated
Normal file
|
@ -0,0 +1,82 @@
|
|||
# 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 = [
|
||||
"libgen",
|
||||
]
|
||||
|
||||
[[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 = "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
lab3/gen_desc/Cargo.toml
Normal file
7
lab3/gen_desc/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "gen_desc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libgen = { path = "../libgen" }
|
19
lab3/gen_desc/src/main.rs
Normal file
19
lab3/gen_desc/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::{env::args, io};
|
||||
|
||||
use libgen::gen_desc;
|
||||
|
||||
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);
|
||||
|
||||
gen_desc(amount)
|
||||
.iter()
|
||||
.for_each(|x| println!("{}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
82
lab3/gen_rand/Cargo.lock
generated
Normal file
82
lab3/gen_rand/Cargo.lock
generated
Normal file
|
@ -0,0 +1,82 @@
|
|||
# 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 = [
|
||||
"libgen",
|
||||
]
|
||||
|
||||
[[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 = "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
lab3/gen_rand/Cargo.toml
Normal file
7
lab3/gen_rand/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "gen_rand"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libgen = { path = "../libgen" }
|
19
lab3/gen_rand/src/main.rs
Normal file
19
lab3/gen_rand/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::{env::args, io};
|
||||
|
||||
use libgen::gen_rand;
|
||||
|
||||
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);
|
||||
|
||||
gen_rand(amount)
|
||||
.iter()
|
||||
.for_each(|x| println!("{}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
75
lab3/libgen/Cargo.lock
generated
Normal file
75
lab3/libgen/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 = "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 = "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
lab3/libgen/Cargo.toml
Normal file
7
lab3/libgen/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "libgen"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
36
lab3/libgen/src/lib.rs
Normal file
36
lab3/libgen/src/lib.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use rand::Rng;
|
||||
|
||||
pub fn gen_rand(amount: u64) -> Vec<u64> {
|
||||
(1..=amount)
|
||||
.map(|_| rand::thread_rng().gen_range(0..(2 * amount)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn gen_desc(amount: u64) -> Vec<u64> {
|
||||
let mut ret = vec![];
|
||||
|
||||
let mut last = rand::thread_rng().gen_range((amount)..(2 * amount));
|
||||
ret.push(last);
|
||||
|
||||
for i in 1..amount {
|
||||
let current = rand::thread_rng().gen_range((amount - i)..(last));
|
||||
ret.push(current);
|
||||
last = current;
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn gen_asc(amount: u64) -> Vec<u64> {
|
||||
let mut ret = vec![];
|
||||
|
||||
let mut last = -1;
|
||||
|
||||
for i in 1..=amount {
|
||||
let current = rand::thread_rng().gen_range((last + 1)..=(2 * amount - (amount - i)) as i64);
|
||||
ret.push(current as u64);
|
||||
last = current;
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
75
lab3/libsort/Cargo.lock
generated
Normal file
75
lab3/libsort/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 = "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.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
|
||||
|
||||
[[package]]
|
||||
name = "libsort"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[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
lab3/libsort/Cargo.toml
Normal file
7
lab3/libsort/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "libsort"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
128
lab3/libsort/src/lib.rs
Normal file
128
lab3/libsort/src/lib.rs
Normal file
|
@ -0,0 +1,128 @@
|
|||
use rand::{thread_rng, Rng};
|
||||
|
||||
fn partition_diff(arr: &mut [u64], lo: usize, hi: usize, pivot: usize) -> usize {
|
||||
let mut i = lo as i64 - 1;
|
||||
for j in lo..hi {
|
||||
if arr[j] == arr[pivot] {
|
||||
i = j as i64;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if i != lo as i64 - 1 {
|
||||
arr.swap(hi - 1, i as usize);
|
||||
}
|
||||
|
||||
let mut k = lo;
|
||||
|
||||
for j in lo..(hi - 1) {
|
||||
if arr[j] < arr[pivot] {
|
||||
arr.swap(j, k);
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
|
||||
arr.swap(hi - 1, k);
|
||||
|
||||
k
|
||||
}
|
||||
|
||||
// pub fn lomuto_partition(arr: &mut [u64], lo: usize, hi: usize, pivot: usize) -> usize {
|
||||
// println!("called lomuto_partition({:?}, {}, {}, {})", arr, lo, hi, pivot);
|
||||
// let mut swap = 0;
|
||||
//
|
||||
// for i in lo..pivot {
|
||||
// if arr[i] < arr[pivot] {
|
||||
// if swap != i {
|
||||
// arr.swap(swap, i);
|
||||
// }
|
||||
// swap += 1;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if swap != pivot {
|
||||
// arr.swap(swap, pivot);
|
||||
// }
|
||||
//
|
||||
// swap
|
||||
// }
|
||||
// pub fn partition(arr: &mut [u64], lo: usize, hi: usize, pivot: usize) -> usize {
|
||||
// println!("called partition({:?}, {}, {}, {})", arr, lo, hi, pivot);
|
||||
// let mut i = lo;
|
||||
// let mut j = hi - 1;
|
||||
//
|
||||
// loop {
|
||||
// while arr[i] < arr[pivot] {
|
||||
// println!("partition i: {i}");
|
||||
// i += 1;
|
||||
// }
|
||||
// while j > 0 && arr[j] > arr[pivot] {
|
||||
// j -= 1;
|
||||
// }
|
||||
// if j == 0 || i >= j {
|
||||
// break;
|
||||
// } else if arr[i] == arr[j] {
|
||||
// i += 1;
|
||||
// j -= 1;
|
||||
// } else {
|
||||
// arr.swap(i, j);
|
||||
// }
|
||||
// }
|
||||
// arr.swap(i, pivot);
|
||||
// i
|
||||
// }
|
||||
|
||||
pub fn rand_partition(arr: &mut [u64], lo: usize, hi: usize) -> usize {
|
||||
let pivot = thread_rng().gen_range(lo..hi);
|
||||
partition_diff(arr, lo, hi, pivot)
|
||||
}
|
||||
|
||||
fn _rand_select(arr: &mut [u64], lo: usize, hi: usize, k: usize) -> (usize, u64) {
|
||||
// println!("called _rand_select({:?}, {}, {}, {})", arr, lo, hi, k);
|
||||
if lo + 1 == hi {
|
||||
return (lo, arr[lo]);
|
||||
}
|
||||
let r = rand_partition(arr, lo, hi);
|
||||
let i = r - lo + 1;
|
||||
if k == i {
|
||||
return (r, arr[r]);
|
||||
} else if k < i {
|
||||
return _rand_select(arr, lo, r - 1, k);
|
||||
} else {
|
||||
return _rand_select(arr, r + 1, hi, k - i);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rand_select(arr: &mut [u64], k: usize) -> (usize, u64) {
|
||||
_rand_select(arr, 0, arr.len() - 1, k)
|
||||
}
|
||||
|
||||
fn _select(arr: &mut [u64], lo: usize, hi: usize, k: usize) -> (usize, u64) {
|
||||
// println!("called _select({:?}, {}, {}, {})", arr, lo, hi, k);
|
||||
if lo + 1 == hi {
|
||||
return (lo, arr[lo]);
|
||||
}
|
||||
|
||||
let mut medians = arr.chunks(5)
|
||||
.map(|chunk| chunk[chunk.len() / 2])
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let n = hi - lo;
|
||||
|
||||
let (pivot, _) = _select(&mut medians, 0, n.div_ceil(5), (n.div_ceil(5) - 1).div_ceil(2));
|
||||
// println!("calling partition from _select");
|
||||
let r = partition_diff(arr, lo, hi, pivot);
|
||||
// println!("_select r: {r}");
|
||||
let i = r - lo + 1;
|
||||
if i == k {
|
||||
return (r, arr[r]);
|
||||
} else if i < k {
|
||||
return _select(arr, r + 1, hi, k - i);
|
||||
} else {
|
||||
return _select(arr, lo, r - 1, k);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(arr: &mut [u64], k: usize) -> (usize, u64) {
|
||||
_select(arr, 0, arr.len() - 1, k)
|
||||
}
|
6
lab3/libsort/src/main.rs
Normal file
6
lab3/libsort/src/main.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use libsort::{select, rand_select};
|
||||
|
||||
fn main() {
|
||||
println!("select: {:?}", select(&mut [3, 1, 9, 20, 3, 5, 31, 8, 29, 34, 2], 5));
|
||||
println!("rand_select: {:?}", rand_select(&mut [3, 1, 9, 20, 3, 5, 31, 8, 29, 34, 2], 5));
|
||||
}
|
Loading…
Reference in a new issue