diff --git a/Cargo.lock b/Cargo.lock index 6b7ce11..8e8877e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,86 @@ +[[package]] +name = "bitflags" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "imposters" version = "0.1.0" +dependencies = [ + "rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.42" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +[metadata] +"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" +"checksum rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6802c0e883716383777e147b7c21323d5de7527257c8b6dc1365a7f2983e90f6" +"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 35c1855..efbd741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] +authors = ["Jason Staten "] name = "imposters" version = "0.1.0" -authors = ["Jason Staten "] - [dependencies] +rand = "0.5.3" diff --git a/src/ch07_algorithms/bench.rs b/src/ch07_algorithms/bench.rs new file mode 100644 index 0000000..a92890b --- /dev/null +++ b/src/ch07_algorithms/bench.rs @@ -0,0 +1,47 @@ +#![allow(unused)] +#![allow(dead_code)] + +extern crate rand; +use super::bubble; +use super::insertion; +use super::selection; +use ch07_algorithms::bench::rand::Rng; + +extern crate test; +use test::{black_box, Bencher}; + +fn generate_data() -> Vec { + let mut rng = rand::thread_rng(); + let mut v = vec![0u32; 1000]; + for x in v.iter_mut() { + *x = rng.gen(); + } + v +} + +#[bench] +fn bench_selection_sort(b: &mut Bencher) { + let data = generate_data(); + b.iter(|| { + let mut data_clone = data.clone(); + black_box(selection::selection_sort(&mut data_clone)) + }); +} + +#[bench] +fn bench_bubble_sort(b: &mut Bencher) { + let data = generate_data(); + b.iter(|| { + let mut data_clone = data.clone(); + black_box(bubble::bubble_sort(&mut data_clone)) + }); +} + +#[bench] +fn bench_insertion_sort(b: &mut Bencher) { + let data = generate_data(); + b.iter(|| { + let mut data_clone = data.clone(); + black_box(insertion::insertion_sort(&mut data_clone)) + }); +} diff --git a/src/ch07_algorithms/bubble.rs b/src/ch07_algorithms/bubble.rs new file mode 100644 index 0000000..7fe36c3 --- /dev/null +++ b/src/ch07_algorithms/bubble.rs @@ -0,0 +1,27 @@ +pub fn bubble_sort(source: &mut [T]) +where + T: Ord, +{ + let mut keep_going = true; + while keep_going { + keep_going = false; + for i in 0..source.len() - 1 { + if source[i] > source[i + 1] { + source.swap(i, i + 1); + keep_going = true; + } + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_small_sort() { + let mut my_vec = vec![100, 42, 73, 24]; + bubble_sort(&mut my_vec); + assert_eq!(my_vec, vec![24, 42, 73, 100]) + } +} diff --git a/src/ch07_algorithms/insertion.rs b/src/ch07_algorithms/insertion.rs new file mode 100644 index 0000000..0536c33 --- /dev/null +++ b/src/ch07_algorithms/insertion.rs @@ -0,0 +1,26 @@ +pub fn insertion_sort(source: &mut [T]) +where + T: Ord, +{ + for i in 0..source.len() { + for j in (0..i).rev() { + if source[j] > source[j + 1] { + source.swap(j, j + 1); + } else { + break; + } + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_small_sort() { + let mut my_vec = vec![100, 42, 73, 24]; + insertion_sort(&mut my_vec); + assert_eq!(my_vec, vec![24, 42, 73, 100]) + } +} diff --git a/src/ch07_algorithms/mod.rs b/src/ch07_algorithms/mod.rs new file mode 100644 index 0000000..9c287ab --- /dev/null +++ b/src/ch07_algorithms/mod.rs @@ -0,0 +1,4 @@ +pub mod bench; +pub mod bubble; +pub mod insertion; +pub mod selection; diff --git a/src/ch07_algorithms/selection.rs b/src/ch07_algorithms/selection.rs new file mode 100644 index 0000000..2ab6734 --- /dev/null +++ b/src/ch07_algorithms/selection.rs @@ -0,0 +1,26 @@ +pub fn selection_sort(source: &mut [T]) +where + T: Ord, +{ + for cur_index in 0..source.len() { + let (min_index, _) = source + .iter() + .enumerate() + .skip(cur_index) + .min_by_key(|&(_, item)| item) + .unwrap(); + source.swap(cur_index, min_index); + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_small_sort() { + let mut my_vec = vec![100, 42, 73, 24]; + selection_sort(&mut my_vec); + assert_eq!(my_vec, vec![24, 42, 73, 100]) + } +} diff --git a/src/lib.rs b/src/lib.rs index 3d12a00..9a45908 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ extern crate test; pub mod ch04_machinery; pub mod ch05_bigo; pub mod ch06_datastructures; +pub mod ch07_algorithms;