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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
#![warn(clippy::all)]

/// The function implements the optimized bubble sort.
/// It sorts the element in the input slice by mutating it.
///
/// This is a exact implementation of the pseudo code from
/// [Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort):
///
/// ```text
/// procedure bubbleSort(A : list of sortable items)
///     n := length(A)
///     repeat
///         newn := 0
///         for i := 1 to n - 1 inclusive do
///             if A[i - 1] > A[i] then
///                 swap(A[i - 1], A[i])
///                 newn := i
///             end if
///         end for
///         n := newn
///     until n ≤ 1
/// end procedure
/// ```
///
/// # Arguments
///
/// * list - The list to be sorted
///
/// # Examples
///
/// ```
/// use bubble_sort::bubble_sort;
///
/// let mut list = [ 2, 3, 5, 4, 1 ];
/// bubble_sort(&mut list);
/// assert_eq!(list, [ 1, 2, 3, 4, 5 ]);
/// ```
///
pub fn bubble_sort<T: PartialOrd>(list: &mut[T]) {
    let mut n = list.len();
    loop {
        let mut lastly_swapped= 0;
        for i in 1..n {
            if list[i - 1] > list[i] {
                list.swap(i - 1, i);
                lastly_swapped = i;
            }
        }
        n = lastly_swapped;
        if n <= 1 {
            break;
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_bubble_sort() {
        use super::bubble_sort;

        let mut list = [4, 8, 9, 2, 3];
        bubble_sort(&mut list);
        assert_eq!(list, [2, 3, 4, 8, 9]);
    }

    #[test]
    fn test_bubble_sort_with_ordered_array() {
        use super::bubble_sort;

        let mut list = [2, 3, 4, 8, 9];
        bubble_sort(&mut list);
        assert_eq!(list, [2, 3, 4, 8, 9]);
    }

    #[test]
    fn test_bubble_sort_with_reverse_ordered_array() {
        use super::bubble_sort;

        let mut list = [9, 8, 4, 3, 2];
        bubble_sort(&mut list);
        assert_eq!(list, [2, 3, 4, 8, 9]);
    }

    #[test]
    fn test_bubble_sort_with_single_element_array() {
        use super::bubble_sort;

        let mut list= [ 42 ];
        bubble_sort(&mut list);
        assert_eq!(list, [ 42 ]);
    }

    #[test]
    fn test_bubble_sort_with_empty_array() {
        use super::bubble_sort;

        let mut list: [usize; 0] = [];
        bubble_sort(&mut list);
        assert_eq!(list, []);
    }

    #[test]
    fn test_bubble_sort_with_vec() {
        use super::bubble_sort;

        let mut list = vec![4, 8, 9, 2, 3];
        bubble_sort(&mut list);
        assert_eq!(list, [2, 3, 4, 8, 9]);
    }
}