Skip to content

Commit 49a8dc1

Browse files
04121613
1 parent 94bc317 commit 49a8dc1

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

exercises/algorithm/algorithm3.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,30 @@
33
This problem requires you to implement a sorting algorithm
44
you can use bubble sorting, insertion sorting, heap sorting, etc.
55
*/
6-
// I AM NOT DONE
76

8-
fn sort<T>(array: &mut [T]) {
9-
//TODO
7+
fn partition<T: Ord>(array: &mut [T], low: isize, high: isize) -> isize {
8+
let piovt = high as usize;
9+
let mut i = low - 1;
10+
for j in low..high {
11+
if array[j as usize] <= array[piovt] {
12+
i += 1;
13+
array.swap(i as usize, j as usize);
14+
}
15+
}
16+
array.swap((i + 1) as usize, piovt);
17+
i + 1
18+
}
19+
20+
fn quick_sort<T: Ord>(array: &mut [T], low: isize, high: isize) {
21+
if low < high {
22+
let piovt = partition(array, low, high);
23+
quick_sort(array, low, piovt - 1);
24+
quick_sort(array, piovt + 1, high);
25+
}
26+
}
27+
28+
fn sort<T: Ord>(array: &mut [T]) {
29+
quick_sort(array, 0, array.len() as isize - 1);
1030
}
1131
#[cfg(test)]
1232
mod tests {

0 commit comments

Comments
 (0)