forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
75 lines (65 loc) · 1.68 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mod bubble_sort;
mod bucket_sort;
mod cocktail_shaker_sort;
mod comb_sort;
mod counting_sort;
mod gnome_sort;
mod heap_sort;
mod insertion_sort;
mod merge_sort;
mod odd_even_sort;
mod pancake_sort;
mod quick_sort;
mod radix_sort;
mod selection_sort;
mod shell_sort;
mod stooge_sort;
mod tim_sort;
pub use self::bubble_sort::bubble_sort;
pub use self::bucket_sort::bucket_sort;
pub use self::cocktail_shaker_sort::cocktail_shaker_sort;
pub use self::comb_sort::comb_sort;
pub use self::counting_sort::counting_sort;
pub use self::counting_sort::generic_counting_sort;
pub use self::gnome_sort::gnome_sort;
pub use self::heap_sort::heap_sort;
pub use self::insertion_sort::insertion_sort;
pub use self::merge_sort::merge_sort;
pub use self::odd_even_sort::odd_even_sort;
pub use self::pancake_sort::pancake_sort;
pub use self::quick_sort::{partition, quick_sort};
pub use self::radix_sort::radix_sort;
pub use self::selection_sort::selection_sort;
pub use self::shell_sort::shell_sort;
pub use self::stooge_sort::stooge_sort;
pub use self::tim_sort::tim_sort;
use std::cmp;
pub fn is_sorted<T>(arr: &[T]) -> bool
where
T: cmp::PartialOrd,
{
if arr.is_empty() {
return true;
}
let mut prev = &arr[0];
for item in arr.iter().skip(1) {
if prev > item {
return false;
}
prev = item;
}
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);
}
}