algo/src/algorithms/bubblesort.rs

18 lines
492 B
Rust
Raw Normal View History

2017-08-27 11:27:35 +00:00
use algorithms::Algorithm;
pub struct Bubblesort;
impl Algorithm for Bubblesort {
2017-08-27 13:51:31 +00:00
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
2017-08-27 11:27:35 +00:00
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;
}
}
}
2017-08-27 13:51:31 +00:00
return Ok(vector);
2017-08-27 11:27:35 +00:00
}
}