Last active
October 5, 2017 18:31
-
-
Save mre/03630c0a09a9d85ac3a9cd278adf9662 to your computer and use it in GitHub Desktop.
Functional style quicksort in Rustlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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