pub fn quick_sort(arr: &mut [i32])
Expand description
Sorts a slice using the Quick Sort algorithm.
Quick Sort is a divide-and-conquer algorithm that picks a ‘pivot’ element from the array, partitions the array around the pivot, and recursively sorts the two subarrays.
§Parameters
arr
: A mutable reference to the slice of integers to be sorted.
§Time Complexity
- Best:
O(n log n)
- Worst:
O(n²)
- Average:
O(n log n)
§Space Complexity
O(log n)
(due to recursion stack)
§Examples
use dsa::algorithms::sorting::quick_sort;
let mut arr = [5, 3, 8, 4, 2];
quick_sort(&mut arr);
assert_eq!(arr, [2, 3, 4, 5, 8]);