Skip to content

Instantly share code, notes, and snippets.

@airled
Last active November 15, 2025 11:45
Show Gist options
  • Select an option

  • Save airled/adeede4ae168b808b0fd35041b1b903a to your computer and use it in GitHub Desktop.

Select an option

Save airled/adeede4ae168b808b0fd35041b1b903a to your computer and use it in GitHub Desktop.
Simple quicksort in rust
fn qs<T: std::cmp::Ord>(a: &mut [T]) {
if a.len() <= 1 { return; }
let last_index = a.len() - 1; // pivot index
let mut partition_index = 0;
for i in 0..last_index {
if a[i] >= a[last_index] { continue; }
a.swap(i, partition_index);
partition_index += 1;
}
a.swap(partition_index, last_index);
qs(&mut a[0..partition_index]);
qs(&mut a[(partition_index + 1)..=last_index]);
}
fn main() {
let mut a = [-1,0,-2,5,7,0,-6,1];
qs(&mut a);
println!("{:?}", a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment