pub fn bubble_sort<T: PartialOrd>(list: &mut [T])
Expand description
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:
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 ]);