Skip to content

Commit e6841e5

Browse files
committed
排序算法-快速排序
1 parent 5c11a69 commit e6841e5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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)