feat: add bubblesort

This commit is contained in:
Jan Christian Grünhage 2017-08-27 13:27:35 +02:00
parent 7172646aaa
commit c2da90822c
4 changed files with 37 additions and 2 deletions

4
Cargo.lock generated Normal file
View file

@ -0,0 +1,4 @@
[root]
name = "sorting"
version = "0.1.0"

View file

@ -0,0 +1,18 @@
use algorithms::Algorithm;
pub struct Bubblesort;
impl Algorithm for Bubblesort {
fn sort(mut vector: Vec<i32>) -> Vec<i32> {
for i in 0..(vector.len()) {
for j in 0..(vector.len() - i - 1) {
if vector[j] > vector[j + 1] {
let tmp = vector[j];
vector[j] = vector[j + 1];
vector[j + 1] = tmp;
}
}
}
return vector;
}
}

6
src/algorithms/mod.rs Normal file
View file

@ -0,0 +1,6 @@
pub trait Algorithm {
fn sort(vector: Vec<i32>) -> Vec<i32>;
}
pub mod bubblesort;

View file

@ -1,3 +1,10 @@
mod algorithms;
use algorithms::bubblesort::Bubblesort;
use algorithms::Algorithm;
fn main() {
println!("Hello, world!");
}
let unsorted : Vec<i32> = vec![2498,29687,24,5,94545,8,51,152,48,871,5];
let sorted = Bubblesort::sort(unsorted);
println!("{:?}", sorted);
}