Last active
November 15, 2025 11:45
-
-
Save airled/adeede4ae168b808b0fd35041b1b903a to your computer and use it in GitHub Desktop.
Simple quicksort 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
| 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