forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
47 lines (41 loc) · 1.37 KB
/
QuickSort.java
File metadata and controls
47 lines (41 loc) · 1.37 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
import java.util.Arrays;
/**
* Description:快速排序
* 过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,
* 则可分别对这两部分记录继续进行排序,以达到整个序列有序。
* Date: 2020-12-26
* Time: 3:56 PM
*/
public class QuickSort {
public static void quickSort(int[] array, int begin, int end) {
if (end <= begin) {
return;
}
int pivot = partition(array, begin, end);
quickSort(array, begin, pivot - 1);
quickSort(array, pivot + 1, end);
}
static int partition(int[] a, int begin, int end) {
// pivot: 标杆位置,counter: 小于pivot的元素的个数
int pivot = end, counter = begin;
for (int i = begin; i < end; i++) {
if (a[i] < a[pivot]) {
if (i != counter) {
swap(a, counter, i);
}
counter++;
}
}
swap(a, pivot, counter);
return counter;
}
private static void swap(int[] arr, int a, int b) {
int tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
public static void main(String[] args) {
QuickSort.quickSort(SortConfig.arr, 0, SortConfig.arr.length - 1);
System.out.println(Arrays.toString(SortConfig.arr));
}
}