Skip to content

Instantly share code, notes, and snippets.

@mre
Last active October 5, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mre/03630c0a09a9d85ac3a9cd278adf9662 to your computer and use it in GitHub Desktop.
Save mre/03630c0a09a9d85ac3a9cd278adf9662 to your computer and use it in GitHub Desktop.
Functional style quicksort in Rustlang
fn quicksort<E: PartialOrd + Clone>(list: &[E]) -> Vec<E> {
let pivot = match list.get(0) {
None => return vec![],
Some(v) => v,
};
let less: Vec<_> = list[1..].iter().filter(|&e| e <= pivot).cloned().collect();
let more: Vec<_> = list.iter().filter(|&e| e > pivot).cloned().collect();
[quicksort(&less), vec![pivot.clone()], quicksort(&more)].concat()
}
fn main() {
assert_eq!(vec![1, 2, 4, 5, 5, 6, 6, 7, 9, 13], quicksort(&[5, 5, 2, 1, 6, 13, 7, 9, 4, 6]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment