-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
30 lines (26 loc) · 739 Bytes
/
lib.rs
File metadata and controls
30 lines (26 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#[no_mangle]
pub extern "C" fn quicksort(arr: *mut i32, len: usize) {
let slice = unsafe { std::slice::from_raw_parts_mut(arr, len) };
quicksort_recursive(slice);
}
fn quicksort_recursive<T: Ord>(arr: &mut [T]) {
if arr.len() <= 1 {
return;
}
let pivot_index = partition(arr);
quicksort_recursive(&mut arr[0..pivot_index]);
quicksort_recursive(&mut arr[pivot_index + 1..]);
}
fn partition<T: Ord>(arr: &mut [T]) -> usize {
let pivot_index = arr.len() / 2;
arr.swap(pivot_index, arr.len() - 1);
let mut i = 0;
for j in 0..arr.len() - 1 {
if arr[j] <= arr[arr.len() - 1] {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, arr.len() - 1);
i
}