Created
January 24, 2022 00:43
-
-
Save cowlicks/df06c72e903549efb89d8fd07036439f to your computer and use it in GitHub Desktop.
Quicksort implemented in Rust
This file contains hidden or 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
use core::fmt::Debug; | |
fn quick_sort<T: Ord + Clone + Debug>(mut list: Vec<T>) -> Vec<T> { | |
if list.len() < 2 { | |
return list; | |
} | |
let pivot = list.pop().unwrap(); | |
let mut left: Vec<T> = vec![]; | |
let mut right: Vec<T> = vec![]; | |
for element in list { | |
if element <= pivot { | |
left.push(element); | |
} else { | |
right.push(element); | |
} | |
} | |
let mut out = quick_sort(left); | |
out.push(pivot); | |
out.append(&mut quick_sort(right)); | |
return out; | |
} | |
fn main() { | |
let v = vec![2, 3, 1, 9]; | |
let v2 = quick_sort(v); | |
println!("{:?}", v2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment