Skip to content

Commit 005915d

Browse files
author
wuyinjun.oreki
committed
update
1 parent cc2ca07 commit 005915d

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

快速排序/main.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
impl Solution {
2-
pub fn sort_array(nums: Vec<i32>) -> Vec<i32> {
2+
pub fn sort_array(num: Vec<i32>) -> Vec<i32> {
3+
return vec![];
4+
}
5+
6+
fn quick_sort(num: &Vec<i32>) -> &Vec<i32> {
7+
if num.len() < 1 {
8+
return &vec![];
9+
}
10+
if num.len() == 1 {
11+
return num;
12+
}
13+
let mid = Solution::partition(&num, 0, num.len() - 1);
14+
let left_slice = Solution::quick_sort(num[..mid]);
15+
let right_slice = Solution::quick_sort(num[mid+1..]);
16+
let temp = [left_slice, vec![num[mid]], right_slice].concat();
17+
return temp;
18+
}
319

20+
fn partition(num: &Vec<i32>, left: usize, right: usize) -> usize {
21+
let pivot = num[right];
22+
let i = left;
23+
for j in left .. right {
24+
if num[j] > pivot {
25+
Solution::swap_here(num, i, j);
26+
i += 1;
27+
}
28+
}
29+
Solution::swap_here(num, i, right);
30+
return i;
431
}
532

6-
fn quick_sort(nums: Vec<i32>) -> Vec<i32> {
7-
33+
fn swap_here(num: &Vec<i32>, x: usize, y: usize) {
34+
let temp = num[x];
35+
num[x] = num[y];
36+
num[y] = temp;
837
}
38+
}
39+
struct Solution {
40+
41+
}
42+
43+
fn main() {
44+
945
}

0 commit comments

Comments
 (0)