Choose the last item as a pivot, partition smaller values to its left, then recurse on the two sides.

Algorithm

The checked-in replay follows the same small input and final output across all 21 DSA books, so this Rust DSA implementation can be compared directly with the other languages.

Basic Implementation

basic.rs
fn partition(arr: &mut [i32], low: usize, high: usize) -> usize {
	let pivot = arr[high];
	let mut i = low;
	for j in low..high {
		if arr[j] <= pivot {
			arr.swap(i, j);
			i += 1;
		}
	}
	arr.swap(i, high);
	i
}

fn quick_sort(arr: &mut [i32], low: usize, high: usize) {
	if low < high {
		let pivot_index = partition(arr, low, high);
		if pivot_index > 0 {
			quick_sort(arr, low, pivot_index - 1);
		}
		quick_sort(arr, pivot_index + 1, high);
	}
}

fn main() {
	let mut arr = [4, 1, 5, 2, 3];
	let last = arr.len() - 1;
	quick_sort(&mut arr, 0, last);
	println!("{:?}", arr);
}

Complexity

  • Time: O(n^2) worst, O(n log n) average
  • Space: O(log n) average call stack
  • Stable: no

Implementation notes

  • Keep the explicit algorithmic steps instead of calling a standard-library sort. The replay is meant to expose comparisons, movement, and recursion.
  • The implementation is intentionally compact for learning and replay, not a production sorting utility.
pivot The final element is moved to the boundary between smaller and larger values.
partition One scan rearranges the current range before the recursive calls.