Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Created June 11, 2015 14:59
Show Gist options
  • Save yohhoy/8f0de840d8af89f10e59 to your computer and use it in GitHub Desktop.
Save yohhoy/8f0de840d8af89f10e59 to your computer and use it in GitHub Desktop.
quick sort
fn quicksort<T: Ord>(mut lst: Vec<T>) -> Vec<T> {
if let Some(pivot) = lst.pop() {
let (less, more): (Vec<_>, Vec<_>) = lst.into_iter().partition(|x| x < &pivot);
let mut res = quicksort(less);
res.push(pivot);
res.extend(quicksort(more));
return res;
} else {
vec![]
}
}
fn main() {
let lst = vec![3, 1, 5, 9, 2, 8, 4, 2, 0, 3, 12, 4, 9, 0, 11];
println!("{:?}", lst);
println!("{:?}", quicksort(lst));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment