forked from Blankj/awesome-java-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
45 lines (40 loc) · 1.14 KB
/
Copy pathQuickSort.java
File metadata and controls
45 lines (40 loc) · 1.14 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
package com.java.practice.sort;
/**
* Created by richard02.zhang on 18/1/26.
*/
public class QuickSort {
private static void swap(int[] arr, int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
private static int partition(int[] arr, int begin, int end) {
int index = begin;
int pivot = arr[end];
for (int i = begin; i < arr.length; i++) {
if (arr[i] < pivot ) {
swap(arr, index, i);
index++;
}
}
swap(arr, end, index);
return index;
}
private static void quickSort(int[] arr, int begin, int end) {
if (null == arr || arr.length == 0) {
return;
}
if (begin < end) {
int index = partition(arr, begin, end);
quickSort(arr, begin, index - 1);
quickSort(arr, index + 1, end);
}
}
public static void main(String[] args) {
int[] arr = {5, 4, 4, 3, 3, 2, 1};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}