198e154495
Add demo code for all currently implemented algorithms, as well as a refactoring of the package structure. Rename of the mistakenly called insertion sort, which was actually selection sort. Fixes #1 and Fixes #2
21 lines
451 B
Rust
21 lines
451 B
Rust
pub trait Algorithm {
|
|
fn sort(vector: Vec<i32>) -> Result<Vec<i32>, &'static str>;
|
|
}
|
|
|
|
|
|
pub mod bubble;
|
|
pub mod merge;
|
|
pub mod selection;
|
|
pub mod quick;
|
|
pub mod insertion;
|
|
|
|
pub fn rotate(mut vector: Vec<i32>, from: usize, to: usize) -> Vec<i32> {
|
|
if from != to {
|
|
let to_value = vector[to];
|
|
for j in (from..(to)).rev() {
|
|
vector[j + 1] = vector[j];
|
|
}
|
|
vector[from] = to_value;
|
|
}
|
|
return vector;
|
|
} |