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
This commit is contained in:
parent
c370cc4486
commit
198e154495
11 changed files with 143 additions and 62 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1,4 +1,4 @@
|
||||||
[root]
|
[root]
|
||||||
name = "sorting"
|
name = "algo"
|
||||||
version = "0.1.0"
|
version = "0.4.0"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "sorting"
|
name = "algo"
|
||||||
version = "0.1.0"
|
version = "0.4.0"
|
||||||
authors = ["Jan Christian Grünhage <jan.christian@gruenhage.xyz>"]
|
authors = ["Jan Christian Grünhage <jan.christian@gruenhage.xyz>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
use algorithms::Algorithm;
|
|
||||||
|
|
||||||
pub struct Insertionsort;
|
|
||||||
|
|
||||||
impl Algorithm for Insertionsort {
|
|
||||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
pub trait Algorithm {
|
|
||||||
fn sort(vector: Vec<i32>) -> Result<Vec<i32>, &'static str>;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub mod bubblesort;
|
|
||||||
pub mod mergesort;
|
|
||||||
pub mod insertionsort;
|
|
||||||
pub mod quicksort;
|
|
90
src/main.rs
90
src/main.rs
|
@ -1,16 +1,82 @@
|
||||||
mod algorithms;
|
mod sorting;
|
||||||
|
|
||||||
use algorithms::quicksort::Quicksort;
|
use sorting::*;
|
||||||
use algorithms::Algorithm;
|
use sorting::Algorithm;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let unsorted: Vec<i32> = vec![2498, 29687, 24, 5, 94545, 8, 51, 152, 48, 871, 5];
|
demo_sorting();
|
||||||
let sorted = match Quicksort::sort(unsorted) {
|
}
|
||||||
Ok(v) => v,
|
|
||||||
Err(e) => {
|
fn demo_sorting() {
|
||||||
println!("Something went wrong! {}", e);
|
let unsorted: Vec<i32> = vec![
|
||||||
return;
|
2498,
|
||||||
}
|
29687,
|
||||||
};
|
24,
|
||||||
println!("{:?}", sorted);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<i32>) -> Result<Vec<i32>, &'static str> {
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||||
for i in 0..(vector.len()) {
|
for i in 0..(vector.len()) {
|
||||||
for j in 0..(vector.len() - i - 1) {
|
for j in 0..(vector.len() - i - 1) {
|
|
@ -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<i32>) -> Result<Vec<i32>, &'static str> {
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||||
if vector.len() == 1 {
|
if vector.len() == 1 {
|
||||||
return Ok(vector);
|
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,
|
Ok(v) => v,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
||||||
second = match Mergesort::sort(second) {
|
second = match Sort::sort(second) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
21
src/sorting/mod.rs
Normal file
21
src/sorting/mod.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
pub trait Algorithm {
|
||||||
|
fn sort(vector: Vec<i32>) -> Result<Vec<i32>, &'static str>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub mod bubble;
|
||||||
|
pub mod merge;
|
||||||
|
pub mod selection;
|
||||||
|
pub mod quick;
|
||||||
|
pub mod insertion;
|
||||||
|
|
||||||
|
pub fn rotate(mut vector: Vec<i32>, from: usize, to: usize) -> Vec<i32> {
|
||||||
|
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;
|
||||||
|
}
|
|
@ -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<i32>) -> Result<Vec<i32>, &'static str> {
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||||
let pivot = match vector.pop() {
|
let pivot = match vector.pop() {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
|
@ -34,7 +34,7 @@ impl Algorithm for Quicksort {
|
||||||
|
|
||||||
let mut result: Vec<i32> = Vec::new();
|
let mut result: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
for v in match Quicksort::sort(smaller) {
|
for v in match Sort::sort(smaller) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ impl Algorithm for Quicksort {
|
||||||
result.push(v);
|
result.push(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
for v in match Quicksort::sort(larger) {
|
for v in match Sort::sort(larger) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
18
src/sorting/selection.rs
Normal file
18
src/sorting/selection.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use sorting::*;
|
||||||
|
|
||||||
|
pub struct Sort;
|
||||||
|
|
||||||
|
impl Algorithm for Sort {
|
||||||
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'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);
|
||||||
|
}
|
||||||
|
}
|
9
src/sorting/template.rs
Normal file
9
src/sorting/template.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use sorting::Algorithm;
|
||||||
|
|
||||||
|
pub struct Sort;
|
||||||
|
|
||||||
|
impl Algorithm for Sort {
|
||||||
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||||
|
return Mergesort::sort(vector);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue