Last active
March 28, 2023 14:57
-
-
Save mykhailokrainik/ed223cef8dca208c2c558c0a43220bef to your computer and use it in GitHub Desktop.
More faster implementation of quick sorting
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 quicksort(arr: &mut [i32]) { | |
if arr.len() <= 1 { | |
return; | |
} | |
let pivot = arr[arr.len() - 1]; | |
let mut i = 0; | |
for j in 0..arr.len() - 1 { | |
if arr[j] < pivot { | |
arr.swap(i, j); | |
i += 1; | |
} | |
} | |
arr.swap(i, arr.len() - 1); | |
quicksort(&mut arr[..i]); | |
quicksort(&mut arr[i + 1..]); | |
} | |
fn main() { | |
let mut array = [10, 5, 2, 3, 8, 7, 9, 1, 4, 6, 5, 11, 14, 8, 2, 9, 10, 1, 3, 4]; | |
quicksort(&mut array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment