25 lines
723 B
Rust
25 lines
723 B
Rust
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;
|
|
}
|
|
}
|
|
return Ok(vector);
|
|
}
|
|
}
|