-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubble.rs
47 lines (39 loc) · 1.33 KB
/
bubble.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
pub fn bubble_sort(unsorted: Vec<u32>) -> Vec<u32> {
let mut sorted = unsorted.clone();
let vector_length = sorted.len();
let mut swapped = false;
// The worst case complexity is O(n*n) where the vector is in reverse
// order and we have to iterate over it n times.
for iteration_number in 0..vector_length - 1 {
// Every iteration, the highest value is moved to the end of the vector,
// so we can ignore that value during next iteration.
for i in 0..vector_length - 1 - iteration_number {
if sorted[i] > sorted[i + 1] {
sorted.swap(i, i + 1);
swapped = true;
}
}
// If no values were swapped, the vector is already sorted and we can
// break early. The best case complexity is O(n).
if !swapped {
break;
}
}
sorted
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sorts_unsorted_vector() {
assert_eq!(bubble_sort(vec![3, 4, 1, 5, 2]), vec![1, 2, 3, 4, 5]);
}
#[test]
fn sorts_reversed_vector() {
assert_eq!(bubble_sort(vec![5, 4, 3, 2, 1]), vec![1, 2, 3, 4, 5]);
}
#[test]
fn sorts_sorted_vector() {
assert_eq!(bubble_sort(vec![1, 2, 3, 4, 5]), vec![1, 2, 3, 4, 5]);
}
}