forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest14.java
More file actions
55 lines (50 loc) · 1.45 KB
/
test14.java
File metadata and controls
55 lines (50 loc) · 1.45 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package byteDance;
/**
* Created by lizeyang on 2020/5/13.
* 快排 so easy~
*/
public class test14 {
//递归实现,时间复杂度O(nlogn),额外空间复杂度O(1),不稳定排序
public static int[] test(int[] nums) {
if (nums == null || nums.length == 0) {
return nums;
}
sort(nums, 0, nums.length - 1);
return nums;
}
public static void sort(int[] nums, int l, int r) {
if (l < r) {
swap(nums, (int) (l + Math.random() * (r - l + 1)), r);
int[] p = partition(nums, l, r);
sort(nums, l, p[0] - 1);
sort(nums, p[1] + 1, r);
}
}
public static int[] partition(int[] nums, int l, int r) {
int less = l - 1;
int more = r;
while (l < more) {
if (nums[l] < nums[r]) {
swap(nums, ++less, l++);
} else if (nums[l] > nums[r]) {
swap(nums, --more, l);
} else {
l++;
}
}
swap(nums, more, r);
return new int[]{less + 1, more};
}
public static void swap(int[] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
public static void main(String[] args) {
int[] nums = {3, 2, 5, 1, 8, 6, 0};
int[] res = test(nums);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}