pub fn bubble_sort(arr: &mut [i32])
Expand description
Sorts a slice using the Bubble Sort algorithm.
The bubble sort algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the list is sorted.
§Parameters
arr
: A mutable reference to the slice of integers to be sorted.
§Time Complexity
- Best:
O(n)
- Worst:
O(n²)
- Average:
O(n²)
§Space Complexity
O(1)
(in-place sorting)
§Examples
use dsa::algorithms::sorting::bubble_sort;
let mut arr = [5, 3, 8, 4, 2];
bubble_sort(&mut arr);
assert_eq!(arr, [2, 3, 4, 5, 8]);