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
//! Implementations of sorting algorithms
pub use bubble_sort::BubbleSort;
pub use insertion_sort::InsertionSort;
pub use quicksort::QuickSort;
mod bubble_sort;
mod insertion_sort;
mod quicksort;

/// Types that implement this trait can sort slices of data
pub trait Sort<T: Ord> {
    /// Sorts a slice of data
    fn sort(arr: &mut [T]);
}

/// Checks if a slice of data is sorted
///
/// # Examples
///
/// ```
/// use lib_wc::sorting::is_sorted;
///
/// let mut arr = [1, 2, 3, 4, 5];
///
/// assert_eq!(true, is_sorted(&mut arr));
///
/// let mut arr = [1, 2, 3, 4, 5, 4];
///
/// assert_eq!(false, is_sorted(&mut arr));
/// ```
pub fn is_sorted<T: Ord>(arr: &[T]) -> bool {
    for i in 1..arr.len() {
        if arr[i - 1] > arr[i] {
            return false;
        }
    }
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_sorted() {
        let mut arr = [1, 2, 3, 4, 5];

        assert_eq!(true, is_sorted(&mut arr));
    }

    #[test]
    fn test_is_not_sorted() {
        let mut arr = [1, 2, 3, 4, 5, 4];
        assert_eq!(false, is_sorted(&mut arr));
    }
}