forked from alexfertel/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.rs
37 lines (32 loc) · 868 Bytes
/
bubble_sort.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
use crate::sorting::traits::Sorter;
fn bubble_sort<T: Ord>(arr: &mut [T]) {
for i in 0..arr.len() {
// Last i elements are already in place.
for j in 0..arr.len() - 1 - i {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
}
/// It sorts the array by repeatedly comparing the
/// adjacent elements and swapping them if they are
/// in the wrong order.
/// Time complexity is O(N^2)
/// Auxiliary space is O(1)
pub struct BubbleSort;
impl<T> Sorter<T> for BubbleSort
where
T: Ord + Copy,
{
fn sort_inplace(array: &mut [T]) {
bubble_sort(array);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::BubbleSort;
sorting_tests!(BubbleSort::sort, bubble_sort);
sorting_tests!(BubbleSort::sort_inplace, bubble_sort, inplace);
}