From b0414be22eecf7911a07954e50e18c80f19cd0c6 Mon Sep 17 00:00:00 2001 From: Alexander Launi Date: Sat, 21 Mar 2020 15:20:34 -0400 Subject: [PATCH] Add Rust examples to 02_selection_sort --- .../rust/01_selection_sort/.gitignore | 10 +++ .../rust/01_selection_sort/Cargo.toml | 7 ++ .../rust/01_selection_sort/src/main.rs | 68 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 02_selection_sort/rust/01_selection_sort/.gitignore create mode 100644 02_selection_sort/rust/01_selection_sort/Cargo.toml create mode 100644 02_selection_sort/rust/01_selection_sort/src/main.rs diff --git a/02_selection_sort/rust/01_selection_sort/.gitignore b/02_selection_sort/rust/01_selection_sort/.gitignore new file mode 100644 index 0000000..50c8301 --- /dev/null +++ b/02_selection_sort/rust/01_selection_sort/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/02_selection_sort/rust/01_selection_sort/Cargo.toml b/02_selection_sort/rust/01_selection_sort/Cargo.toml new file mode 100644 index 0000000..673ae94 --- /dev/null +++ b/02_selection_sort/rust/01_selection_sort/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "selection-sort" +version = "0.1.0" +authors = ["Alexander Launi "] +edition = "2018" + +[dependencies] diff --git a/02_selection_sort/rust/01_selection_sort/src/main.rs b/02_selection_sort/rust/01_selection_sort/src/main.rs new file mode 100644 index 0000000..2272dd0 --- /dev/null +++ b/02_selection_sort/rust/01_selection_sort/src/main.rs @@ -0,0 +1,68 @@ +// Swaps elements in an array +fn swap(list: &mut [T], index_a: usize, index_b: usize) { + let tmp = list[index_a]; + list[index_a] = list[index_b]; + list[index_b] = tmp; +} + +fn selection_sort(list: &mut [T]) { + for i in 0..list.len() { + for j in (i+1)..list.len() { + if list[j] > list[i] { + swap(list, i, j); + } + } + } +} + +fn main() { + let list = &mut [156, 141, 35, 94, 88, 61, 111]; + print!("{:?} => ", list); + selection_sort(list); + print!("{:?}\n", list); +} + +#[cfg(test)] +mod test { + use super::selection_sort; + + #[test] + fn sort_unsigned_list() { + let mut list : [u8; 7] = [156, 141, 35, 94, 88, 61, 111]; + let list_sorted : [u8; 7] = [156, 141, 111, 94, 88, 61, 35]; + selection_sort(&mut list); + + assert_eq!(list, list_sorted); + } + + #[test] + fn sort_signed_list() { + let mut list : [i32; 10] = [75, 85, -26, 61, 20, -40, -72, 30, -27, 58]; + let list_sorted : [i32; 10] = [85, 75, 61, 58, 30, 20, -26, -27, -40, -72]; + selection_sort(&mut list); + + assert_eq!(list, list_sorted); + } + + #[test] + fn sort_strings() { + let mut list : [&str; 7] = ["Radiohead", "Kishore Kumar", "The Black Keys", + "Neutral Milk Hotel", "Beck", "The Strokes", "Wilco" + ]; + let list_sorted : [&str; 7] = ["Wilco", "The Strokes", "The Black Keys", + "Radiohead", "Neutral Milk Hotel", "Kishore Kumar", "Beck" + ]; + selection_sort(&mut list); + + assert_eq!(list, list_sorted); + } + + #[test] + fn sorts_an_empty_list() { + let mut list : [u8; 0] = []; + let list_sorted : [u8; 0] = []; + selection_sort(&mut list); + + assert_eq!(list, list_sorted); + } +} \ No newline at end of file