-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
56 lines (48 loc) · 1.67 KB
/
mod.rs
File metadata and controls
56 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::time::Instant;
pub mod bogo_sort;
pub mod bubble_sort;
mod constants;
pub mod heap_sort;
pub mod insertion_sort;
pub mod merge_sort;
pub mod selection_sort;
/// A Sorter is a sorting algorithm split in two stages: the `step` and the `state`.
/// A `step` can be any single step an algorithm takes, such as comparing or switching numbers
/// A `state` controls the variables that the `step` is going to use.
pub trait Sorter {
fn new() -> Self
// The Compiler will complain if we don't do this
where
Self: Sized;
/// Returns the indexes currently being compared or about to switch.
fn special(&self) -> (usize, usize);
/// Returns the reason the special indexes are special.
fn reason(&self) -> Reasons;
/// Loops all states and reset state.
/// Returns time elapsed (microsseconds).
fn run(&mut self, array: &mut Vec<usize>) -> u128 {
let now = Instant::now();
loop {
if self.step(array) {
break;
}
}
self.reset_state();
now.elapsed().as_micros()
}
/// Takes a single step in running the algorithm.
/// Returns true if all states have been covered.
fn step(&mut self, array: &mut Vec<usize>) -> bool;
/// Modifying the state is analogous to stepping in a loop.
/// Returns true if all states have been traversed.
fn modify_state(&mut self, array: &[usize]) -> bool;
/// Handles switching positions in an array
fn switch(&mut self, array: &mut Vec<usize>);
/// Set the Sorter's state to it's initial state.
fn reset_state(&mut self);
}
#[derive(PartialEq, Clone, Copy)]
pub enum Reasons {
Comparing,
Switching,
}