forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
53 lines (43 loc) · 1.04 KB
/
mod.rs
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
mod bubble_sort;
mod counting_sort;
mod heap_sort;
mod insertion;
mod quick_sort;
mod selection_sort;
use std::cmp;
pub use self::bubble_sort::bubble_sort;
pub use self::counting_sort::counting_sort;
pub use self::counting_sort::generic_counting_sort;
pub use self::heap_sort::heap_sort;
pub use self::insertion::insertion_sort;
pub use self::quick_sort::quick_sort;
pub use self::selection_sort::selection_sort;
pub fn is_sorted<T>(arr: &[T]) -> bool
where
T: cmp::PartialOrd,
{
if arr.is_empty() {
return true;
}
let mut prev = &arr[0];
for idx in 1..arr.len() {
if prev > &arr[idx] {
return false;
}
prev = &arr[idx];
}
true
}
#[cfg(test)]
mod tests {
#[test]
fn is_sorted() {
use super::*;
assert!(is_sorted(&[] as &[isize]));
assert!(is_sorted(&["a"]));
assert!(is_sorted(&[1, 2, 3]));
assert!(is_sorted(&[0, 1, 1]));
assert_eq!(is_sorted(&[1, 0]), false);
assert_eq!(is_sorted(&[2, 3, 1, -1, 5]), false);
}
}