feat: add mergesort
This commit is contained in:
parent
451101c7ae
commit
df06ce02c9
|
@ -3,7 +3,7 @@ use algorithms::Algorithm;
|
||||||
pub struct Bubblesort;
|
pub struct Bubblesort;
|
||||||
|
|
||||||
impl Algorithm for Bubblesort {
|
impl Algorithm for Bubblesort {
|
||||||
fn sort(mut vector: Vec<i32>) -> Vec<i32> {
|
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) {
|
||||||
if vector[j] > vector[j + 1] {
|
if vector[j] > vector[j + 1] {
|
||||||
|
@ -13,6 +13,6 @@ impl Algorithm for Bubblesort {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector;
|
return Ok(vector);
|
||||||
}
|
}
|
||||||
}
|
}
|
60
src/algorithms/mergesort.rs
Normal file
60
src/algorithms/mergesort.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
use algorithms::Algorithm;
|
||||||
|
|
||||||
|
pub struct Mergesort;
|
||||||
|
|
||||||
|
impl Algorithm for Mergesort {
|
||||||
|
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||||
|
if vector.len() == 1 {
|
||||||
|
return Ok(vector);
|
||||||
|
} else {
|
||||||
|
let mut second: Vec<i32> = Vec::new();
|
||||||
|
let length = vector.len();
|
||||||
|
for _ in 0..length / 2 {
|
||||||
|
second.push(match vector.pop() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => {
|
||||||
|
return Err("Error during splitting the vector.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let first = match Mergesort::sort(vector) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
};
|
||||||
|
second = match Mergesort::sort(second) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut result: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
|
let first_length = first.len();
|
||||||
|
let second_length = second.len();
|
||||||
|
|
||||||
|
let mut first_index = 0;
|
||||||
|
let mut second_index = 0;
|
||||||
|
|
||||||
|
while first_index < first_length && second_index < second_length {
|
||||||
|
if first[first_index] < second[second_index] {
|
||||||
|
result.push(first[first_index]);
|
||||||
|
first_index += 1;
|
||||||
|
} else {
|
||||||
|
result.push(second[second_index]);
|
||||||
|
second_index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while first_index < first_length {
|
||||||
|
result.push(first[first_index]);
|
||||||
|
first_index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while second_index < second_length {
|
||||||
|
result.push(second[second_index]);
|
||||||
|
second_index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
pub trait Algorithm {
|
pub trait Algorithm {
|
||||||
fn sort(vector: Vec<i32>) -> Vec<i32>;
|
fn sort(vector: Vec<i32>) -> Result<Vec<i32>, &'static str>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub mod bubblesort;
|
pub mod bubblesort;
|
||||||
|
pub mod mergesort;
|
10
src/main.rs
10
src/main.rs
|
@ -1,10 +1,16 @@
|
||||||
mod algorithms;
|
mod algorithms;
|
||||||
|
|
||||||
use algorithms::bubblesort::Bubblesort;
|
use algorithms::mergesort::Mergesort;
|
||||||
use algorithms::Algorithm;
|
use algorithms::Algorithm;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let unsorted : Vec<i32> = vec![2498,29687,24,5,94545,8,51,152,48,871,5];
|
let unsorted : Vec<i32> = vec![2498,29687,24,5,94545,8,51,152,48,871,5];
|
||||||
let sorted = Bubblesort::sort(unsorted);
|
let sorted = match Mergesort::sort(unsorted) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
println!("Something went wrong! {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
println!("{:?}", sorted);
|
println!("{:?}", sorted);
|
||||||
}
|
}
|
Loading…
Reference in a new issue