algorithms_rs/sort/
mod.rs

1mod bubble_sort;
2pub use bubble_sort::*;
3mod select_sort;
4pub use select_sort::*;
5mod insert_sort;
6pub use insert_sort::*;
7mod merge_sort;
8pub use merge_sort::*;
9
10/// Generic interface to sorting algorithms
11pub trait Sort<T: core::cmp::PartialOrd + Clone> {
12    /// Get the internal data
13    fn inner(&self) -> Vec<T>;
14
15    /// Customizable comparison logic based on closures
16    fn sort_by<F>(&mut self, f: F)
17    where
18        F: FnOnce(&T, &T) -> bool + core::marker::Copy;
19
20    /// Sort by ascending order
21    fn sort(&mut self) {
22        self.sort_by(|v1, v2| v1 <= v2);
23    }
24
25    /// Determine if the sort is ascending
26    fn is_sort(&self) -> bool {
27        self.is_sort_by(|v1, v2| v1 <= v2)
28    }
29
30    /// Customized judgments are ordered
31    fn is_sort_by<F>(&self, f: F) -> bool
32    where
33        F: FnOnce(&T, &T) -> bool + core::marker::Copy,
34    {
35        let array = self.inner();
36
37        if array.is_empty() {
38            return true;
39        }
40
41        for idx in 0..array.len() - 1 {
42            if !f(&array[idx], &array[idx + 1]) {
43                return false;
44            }
45        }
46        true
47    }
48}
49
50pub trait Infite {
51    fn max_value() -> Self;
52
53    fn min_value() -> Self;
54}