-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathQuickSort.java
79 lines (71 loc) · 1.67 KB
/
QuickSort.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Copyright 2022 jingedawang
*/
package sort;
import utils.ArrayPrinter;
import utils.ArrayGenerator;
/**
* Quick sort algorithm.
*/
public class QuickSort implements Sort {
public static void main(String[] args) {
int[] arr = ArrayGenerator.fixedArray();
System.out.println("Original array:");
ArrayPrinter.print(arr);
Sort sort = new QuickSort();
sort.sort(arr);
System.out.println();
System.out.println("Sorted by quick sort:");
ArrayPrinter.print(arr);
}
/**
* Quick sort.
*
* @param arr Integer array to be sorted.
*/
@Override
public void sort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
/**
* The recursive procedure in quick sort.
*
* @param arr The array to be sorted.
* @param p The start index of the sub-array to be sorted.
* @param r The end index of the sub-array to be sorted.
*/
protected void quickSort(int[] arr, int p, int r) {
if (p < r) {
int q = partition(arr, p, r);
quickSort(arr, p, q - 1);
quickSort(arr, q + 1, r);
}
}
/**
* Partition the array into two parts.
* <p>
* After this method, elements greater than pivot are put right, others are put left.
*
* @param arr The array to be sorted.
* @param p The start index of the sub-array to be sorted.
* @param r The end index of the sub-array to be sorted.
* @return The index of the pivot after partition.
*/
protected int partition(int[] arr, int p, int r) {
int x = arr[r];
int i = p - 1;
int temp;
for (int j = p; j < r; j++) {
if (arr[j] <= x) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[r];
arr[r] = temp;
return i + 1;
}
}