MCPcopy Index your code
hub / github.com/hustcc/JS-Sorting-Algorithm / QuickSort

Class QuickSort

src/java/main/QuickSort.java:6–45  ·  view source on GitHub ↗

快速排序

Source from the content-addressed store, hash-verified

4 * 快速排序
5 */
6public class QuickSort implements IArraySort {
7
8 @Override
9 public int[] sort(int[] sourceArray) throws Exception {
10 // 对 arr 进行拷贝,不改变参数内容
11 int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
12
13 return quickSort(arr, 0, arr.length - 1);
14 }
15
16 private int[] quickSort(int[] arr, int left, int right) {
17 if (left < right) {
18 int partitionIndex = partition(arr, left, right);
19 quickSort(arr, left, partitionIndex - 1);
20 quickSort(arr, partitionIndex + 1, right);
21 }
22 return arr;
23 }
24
25 private int partition(int[] arr, int left, int right) {
26 // 设定基准值(pivot)
27 int pivot = left;
28 int index = pivot + 1;
29 for (int i = index; i <= right; i++) {
30 if (arr[i] < arr[pivot]) {
31 swap(arr, i, index);
32 index++;
33 }
34 }
35 swap(arr, pivot, index - 1);
36 return index - 1;
37 }
38
39 private void swap(int[] arr, int i, int j) {
40 int temp = arr[i];
41 arr[i] = arr[j];
42 arr[j] = temp;
43 }
44
45}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected