Compare commits

...

3 commits

Author SHA1 Message Date
Jan Christian Grünhage 9f847fe038 docs: update changelog 2017-08-27 17:27:52 +02:00
Jan Christian Grünhage 6659387d40 feat: add insertionsort 2017-08-27 17:26:29 +02:00
Jan Christian Grünhage 62f1b27dff refactor: cargo fmt 2017-08-27 17:26:01 +02:00
6 changed files with 45 additions and 9 deletions

View file

@ -1,3 +1,13 @@
<a name="v0.3.0"></a>
## v0.3.0 (2017-08-27)
#### Features
* add insertionsort ([6659387d](6659387d))
<a name="v0.2.0"></a>
## v0.2.0 (2017-08-27)

View file

@ -15,4 +15,4 @@ impl Algorithm for Bubblesort {
}
return Ok(vector);
}
}
}

View file

@ -0,0 +1,25 @@
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;
}
println!("{:?}", vector);
}
return Ok(vector);
}
}

View file

@ -19,11 +19,11 @@ impl Algorithm for Mergesort {
}
let first = match Mergesort::sort(vector) {
Ok(v) => v,
Err(e) => return Err(e)
Err(e) => return Err(e),
};
second = match Mergesort::sort(second) {
Ok(v) => v,
Err(e) => return Err(e)
Err(e) => return Err(e),
};
let mut result: Vec<i32> = Vec::new();
@ -57,4 +57,4 @@ impl Algorithm for Mergesort {
return Ok(result);
}
}
}
}

View file

@ -4,4 +4,5 @@ pub trait Algorithm {
pub mod bubblesort;
pub mod mergesort;
pub mod mergesort;
pub mod insertionsort;

View file

@ -1,11 +1,11 @@
mod algorithms;
use algorithms::mergesort::Mergesort;
use algorithms::insertionsort::Insertionsort;
use algorithms::Algorithm;
fn main() {
let unsorted : Vec<i32> = vec![2498,29687,24,5,94545,8,51,152,48,871,5];
let sorted = match Mergesort::sort(unsorted) {
let unsorted: Vec<i32> = vec![2498, 29687, 24, 5, 94545, 8, 51, 152, 48, 871, 5];
let sorted = match Insertionsort::sort(unsorted) {
Ok(v) => v,
Err(e) => {
println!("Something went wrong! {}", e);
@ -13,4 +13,4 @@ fn main() {
}
};
println!("{:?}", sorted);
}
}