From 198e15449520db55e24fc61333ba4ba2b20eb94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Tue, 29 Aug 2017 10:08:42 +0200 Subject: [PATCH] feat: demo all algorithms Add demo code for all currently implemented algorithms, as well as a refactoring of the package structure. Rename of the mistakenly called insertion sort, which was actually selection sort. Fixes #1 and Fixes #2 --- Cargo.lock | 4 +- Cargo.toml | 4 +- src/algorithms/insertionsort.rs | 24 ----- src/algorithms/mod.rs | 9 -- src/main.rs | 90 ++++++++++++++++--- .../bubblesort.rs => sorting/bubble.rs} | 6 +- .../mergesort.rs => sorting/merge.rs} | 10 +-- src/sorting/mod.rs | 21 +++++ .../quicksort.rs => sorting/quick.rs} | 10 +-- src/sorting/selection.rs | 18 ++++ src/sorting/template.rs | 9 ++ 11 files changed, 143 insertions(+), 62 deletions(-) delete mode 100644 src/algorithms/insertionsort.rs delete mode 100644 src/algorithms/mod.rs rename src/{algorithms/bubblesort.rs => sorting/bubble.rs} (83%) rename src/{algorithms/mergesort.rs => sorting/merge.rs} (89%) create mode 100644 src/sorting/mod.rs rename src/{algorithms/quicksort.rs => sorting/quick.rs} (88%) create mode 100644 src/sorting/selection.rs create mode 100644 src/sorting/template.rs diff --git a/Cargo.lock b/Cargo.lock index 76251c9..3e36502 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,4 @@ [root] -name = "sorting" -version = "0.1.0" +name = "algo" +version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 7054eac..1626dfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "sorting" -version = "0.1.0" +name = "algo" +version = "0.4.0" authors = ["Jan Christian Grünhage "] [dependencies] diff --git a/src/algorithms/insertionsort.rs b/src/algorithms/insertionsort.rs deleted file mode 100644 index 425935f..0000000 --- a/src/algorithms/insertionsort.rs +++ /dev/null @@ -1,24 +0,0 @@ -use algorithms::Algorithm; - -pub struct Insertionsort; - -impl Algorithm for Insertionsort { - fn sort(mut vector: Vec) -> Result, &'static str> { - for i in 0..vector.len() { - let mut smallest_index = i; - for j in i..vector.len() { - if vector[j] < vector[smallest_index] { - smallest_index = j; - } - } - if i != smallest_index { - let smallest_value = vector[smallest_index]; - for j in (i..(smallest_index)).rev() { - vector[j + 1] = vector[j]; - } - vector[i] = smallest_value; - } - } - return Ok(vector); - } -} diff --git a/src/algorithms/mod.rs b/src/algorithms/mod.rs deleted file mode 100644 index 9d48bae..0000000 --- a/src/algorithms/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -pub trait Algorithm { - fn sort(vector: Vec) -> Result, &'static str>; -} - - -pub mod bubblesort; -pub mod mergesort; -pub mod insertionsort; -pub mod quicksort; diff --git a/src/main.rs b/src/main.rs index 9568228..7008a9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,82 @@ -mod algorithms; +mod sorting; -use algorithms::quicksort::Quicksort; -use algorithms::Algorithm; +use sorting::*; +use sorting::Algorithm; fn main() { - let unsorted: Vec = vec![2498, 29687, 24, 5, 94545, 8, 51, 152, 48, 871, 5]; - let sorted = match Quicksort::sort(unsorted) { - Ok(v) => v, - Err(e) => { - println!("Something went wrong! {}", e); - return; - } - }; - println!("{:?}", sorted); + demo_sorting(); +} + +fn demo_sorting() { + let unsorted: Vec = vec![ + 2498, + 29687, + 24, + 94545, + 8, + 51, + 152, + 871, + 5, + 6557, + 216, + 75986, + 48, + 526, + 15, + 6, + 78, + ]; + + println!("Unsorted array: {:?}", unsorted); + println!( + "Bubblesort : {:?}", + match bubble::Sort::sort(unsorted.clone()) { + Ok(v) => v, + Err(e) => { + println!("Something went wrong! {}", e); + return; + } + } + ); + println!( + "Selectionsort : {:?}", + match selection::Sort::sort(unsorted.clone()) { + Ok(v) => v, + Err(e) => { + println!("Something went wrong! {}", e); + return; + } + } + ); + println!( + "Insertionsort : {:?}", + match insertion::Sort::sort(unsorted.clone()) { + Ok(v) => v, + Err(e) => { + println!("Something went wrong! {}", e); + return; + } + } + ); + println!( + "Mergesort : {:?}", + match merge::Sort::sort(unsorted.clone()) { + Ok(v) => v, + Err(e) => { + println!("Something went wrong! {}", e); + return; + } + } + ); + println!( + "Quicksort : {:?}", + match quick::Sort::sort(unsorted.clone()) { + Ok(v) => v, + Err(e) => { + println!("Something went wrong! {}", e); + return; + } + } + ); } diff --git a/src/algorithms/bubblesort.rs b/src/sorting/bubble.rs similarity index 83% rename from src/algorithms/bubblesort.rs rename to src/sorting/bubble.rs index 07938a5..3eb3667 100644 --- a/src/algorithms/bubblesort.rs +++ b/src/sorting/bubble.rs @@ -1,8 +1,8 @@ -use algorithms::Algorithm; +use sorting::Algorithm; -pub struct Bubblesort; +pub struct Sort; -impl Algorithm for Bubblesort { +impl Algorithm for Sort { fn sort(mut vector: Vec) -> Result, &'static str> { for i in 0..(vector.len()) { for j in 0..(vector.len() - i - 1) { diff --git a/src/algorithms/mergesort.rs b/src/sorting/merge.rs similarity index 89% rename from src/algorithms/mergesort.rs rename to src/sorting/merge.rs index 14a0e3a..f194d7a 100644 --- a/src/algorithms/mergesort.rs +++ b/src/sorting/merge.rs @@ -1,8 +1,8 @@ -use algorithms::Algorithm; +use sorting::Algorithm; -pub struct Mergesort; +pub struct Sort; -impl Algorithm for Mergesort { +impl Algorithm for Sort { fn sort(mut vector: Vec) -> Result, &'static str> { if vector.len() == 1 { return Ok(vector); @@ -17,11 +17,11 @@ impl Algorithm for Mergesort { } }); } - let first = match Mergesort::sort(vector) { + let first = match Sort::sort(vector) { Ok(v) => v, Err(e) => return Err(e), }; - second = match Mergesort::sort(second) { + second = match Sort::sort(second) { Ok(v) => v, Err(e) => return Err(e), }; diff --git a/src/sorting/mod.rs b/src/sorting/mod.rs new file mode 100644 index 0000000..be1c5ec --- /dev/null +++ b/src/sorting/mod.rs @@ -0,0 +1,21 @@ +pub trait Algorithm { + fn sort(vector: Vec) -> Result, &'static str>; +} + + +pub mod bubble; +pub mod merge; +pub mod selection; +pub mod quick; +pub mod insertion; + +pub fn rotate(mut vector: Vec, from: usize, to: usize) -> Vec { + if from != to { + let to_value = vector[to]; + for j in (from..(to)).rev() { + vector[j + 1] = vector[j]; + } + vector[from] = to_value; + } + return vector; +} \ No newline at end of file diff --git a/src/algorithms/quicksort.rs b/src/sorting/quick.rs similarity index 88% rename from src/algorithms/quicksort.rs rename to src/sorting/quick.rs index 71c9a75..1d8e51b 100644 --- a/src/algorithms/quicksort.rs +++ b/src/sorting/quick.rs @@ -1,8 +1,8 @@ -use algorithms::Algorithm; +use sorting::Algorithm; -pub struct Quicksort; +pub struct Sort; -impl Algorithm for Quicksort { +impl Algorithm for Sort { fn sort(mut vector: Vec) -> Result, &'static str> { let pivot = match vector.pop() { Some(v) => v, @@ -34,7 +34,7 @@ impl Algorithm for Quicksort { let mut result: Vec = Vec::new(); - for v in match Quicksort::sort(smaller) { + for v in match Sort::sort(smaller) { Ok(v) => v, Err(e) => return Err(e), } @@ -46,7 +46,7 @@ impl Algorithm for Quicksort { result.push(v); } - for v in match Quicksort::sort(larger) { + for v in match Sort::sort(larger) { Ok(v) => v, Err(e) => return Err(e), } diff --git a/src/sorting/selection.rs b/src/sorting/selection.rs new file mode 100644 index 0000000..f4c2457 --- /dev/null +++ b/src/sorting/selection.rs @@ -0,0 +1,18 @@ +use sorting::*; + +pub struct Sort; + +impl Algorithm for Sort { + fn sort(mut vector: Vec) -> Result, &'static str> { + for i in 0..vector.len() { + let mut smallest_index = i; + for j in i..vector.len() { + if vector[j] < vector[smallest_index] { + smallest_index = j; + } + } + vector = rotate(vector, i, smallest_index); + } + return Ok(vector); + } +} diff --git a/src/sorting/template.rs b/src/sorting/template.rs new file mode 100644 index 0000000..56b2beb --- /dev/null +++ b/src/sorting/template.rs @@ -0,0 +1,9 @@ +use sorting::Algorithm; + +pub struct Sort; + +impl Algorithm for Sort { + fn sort(mut vector: Vec) -> Result, &'static str> { + return Mergesort::sort(vector); + } +}