feat: add bubblesort
This commit is contained in:
parent
7172646aaa
commit
c2da90822c
4 changed files with 37 additions and 2 deletions
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[root]
|
||||
name = "sorting"
|
||||
version = "0.1.0"
|
||||
|
18
src/algorithms/bubblesort.rs
Normal file
18
src/algorithms/bubblesort.rs
Normal 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
6
src/algorithms/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
pub trait Algorithm {
|
||||
fn sort(vector: Vec<i32>) -> Vec<i32>;
|
||||
}
|
||||
|
||||
|
||||
pub mod bubblesort;
|
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue