Skip to content

Commit 884e86f

Browse files
committed
Merge remote-tracking branch 'origin/main' into main
2 parents 9645da9 + 783755f commit 884e86f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/com/gatsby/_912SortAnArray.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.gatsby;
2+
3+
import com.gatsby.utils.SortUtil;
4+
5+
/**
6+
* @ClassName: _912SortAnArray
7+
* @Description: leetcode 912 排序数组
8+
* @author: Gatsby
9+
* @date: 2022/7/25 15:23
10+
*/
11+
12+
public class _912SortAnArray {
13+
public int[] sortArray(int[] nums) {
14+
SortUtil.quickSort(nums, 0, nums.length - 1);
15+
return nums;
16+
}
17+
}
18+
19+
20+

src/com/gatsby/utils/SortUtil.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.gatsby.utils;
2+
3+
/**
4+
* @ClassName: SortUtil
5+
* @Description: 排序类
6+
* @author: Gatsby
7+
* @date: 2022/7/25 15:03
8+
*/
9+
10+
public class SortUtil {
11+
/**
12+
* @MethodName: quickSort
13+
* @Parameter: [nums, begin, end]
14+
* @Return void
15+
* @Description: 快速排序
16+
* @author: Gatsby
17+
* @date: 2022/7/25 15:20
18+
*/
19+
public static void quickSort(int[] nums, int begin, int end) {
20+
if (begin < end) {
21+
int pivot = nums[begin];
22+
int i = begin;
23+
int j = end;
24+
while (i < j) {
25+
while (i < j && nums[j] > pivot) {
26+
j--;
27+
}
28+
if (i < j) {
29+
nums[i] = nums[j];
30+
i++;
31+
}
32+
33+
while (i < j && nums[i] < pivot) {
34+
i++;
35+
}
36+
if (i < j) {
37+
nums[j] = nums[i];
38+
j--;
39+
}
40+
}
41+
nums[j] = pivot;
42+
quickSort(nums, begin, j - 1);
43+
quickSort(nums, j + 1, end);
44+
}
45+
}
46+
}
47+
48+

0 commit comments

Comments
 (0)