Skip to content

Instantly share code, notes, and snippets.

@misha-krainik
Last active March 28, 2023 14:57
Show Gist options
  • Save misha-krainik/ed223cef8dca208c2c558c0a43220bef to your computer and use it in GitHub Desktop.
Save misha-krainik/ed223cef8dca208c2c558c0a43220bef to your computer and use it in GitHub Desktop.
More faster implementation of quick sorting
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