-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
45 lines (37 loc) · 1.15 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
public class QuickSort {
public void quickSort(int arr[], int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex-1);
quickSort(arr, partitionIndex+1, end);
}
}
private int partition(int arr[], int begin, int end) {
int pivot = arr[end];
int i = (begin-1);
for (int j = begin; j < end; j++) {
if (arr[j] <= pivot) {
i++;
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}
int swapTemp = arr[i+1];
arr[i+1] = arr[end];
arr[end] = swapTemp;
return i+1;
}
public static void main(String[] args) {
int[] nums = new int[]{12,4,5,6,2,15,167,7,16,27,25,87,26};
QuickSort B1 = new QuickSort();
B1.quickSort(nums, 0, nums.length - 1);
for (int num : nums) {
System.out.println(num);
}
System.out.println("*************");
for (int num : nums) {
System.out.println(num);
}
}
}