-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQuickSort.java
198 lines (166 loc) · 4.64 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package sorting;
public class QuickSort {
public static class QuickSortPartitionLast {
public int[] quickSort(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
private void quickSort(int[] nums, int low, int high) {
if (low < high) {
int pi = partitionLast(nums, low, high);
quickSort(nums, low, pi - 1);
quickSort(nums, pi + 1, high);
}
}
private int partitionLast(int[] nums, int low, int high) {
int pivot = nums[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (nums[j] <= pivot) {
i++;
//swap nums[i] and nums[j]
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
//swap nums[i+1] and nums[high]
int temp = nums[i + 1];
nums[i + 1] = nums[high];
nums[high] = temp;
return i + 1;
}
}
public static class QuickSortPartitionFirst {
public int[] quickSort(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
private void quickSort(int[] nums, int low, int high) {
if (low < high) {
int pi = partitionFirst(nums, low, high);
quickSort(nums, low, pi - 1);
quickSort(nums, pi + 1, high);
}
}
// this method find the pivot and do the sorting
private int partitionFirst(int[] list, int first, int last) {
int pivot = list[first];
int low = first + 1; // searching forward from pivot'next element
int high = last;// searching from the end for backward
while (low < high) {
// forward searching
while (low <= high && list[low] < pivot)
low++;
// backward searching
while (low <= high && list[high] >= pivot)
high--;
// swap two elements in the list when list[high]<pivot
if (high > low) {
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while (high > first && list[high] >= pivot)
high--;
// swap pivot
if (pivot > list[high]) {
list[first] = list[high];
list[high] = pivot;
return high;
} else {
return first;
}
}
}
public static void main(String... args){
int[] nums = InsertionSort.generateRandomArray(17);
// int[] nums = new int[]{10, 7, 8, 9, 1, 5};
SortUtils.print(nums);
// test.swap(nums, 2, 0);
// test.print(nums);
QuickSort test = new QuickSort();
// InsertionSort.print(test.quickSort(nums));
// InsertionSort.print(test.quickSort_20160628(nums));
// InsertionSort.print(test.quickSort_pivot_first_20160628(nums));
SortUtils.print(test.quickSort_pivot_median_20160628(nums));
}
int[] quickSort_20160628(int[] nums){
quickSort_20160628(nums, 0, nums.length-1);
return nums;
}
void quickSort_20160628(int[] nums, int low, int high) {
if(low < high){
int pi = partition_20160628(nums, low, high);
quickSort_20160628(nums, low, pi-1);
quickSort_20160628(nums, pi+1, high);
}
}
int partition_20160628(int[] nums, int low, int high) {
int pivot = nums[high];
int i = low-1;
for(int j = low; j <= high-1; j++){
if(nums[j] <= pivot){
i++;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
int temp = nums[i+1];
nums[i+1] = nums[high];
nums[high] = temp;
return i+1;
}
//This below method is also accepted on http://www.practice.geeksforgeeks.org/problem-page.php?pid=700151
int[] quickSort_pivot_first_20160628(int[] nums){
quickSort_pivot_first_20160628(nums, 0, nums.length-1);
return nums;
}
private void quickSort_pivot_first_20160628(int[] nums, int low, int high) {
if (low < high) {
int pi = partition_pivot_first_20160628(nums, low, high);
quickSort_pivot_first_20160628(nums, low, pi - 1);
quickSort_pivot_first_20160628(nums, pi + 1, high);
}
}
private int partition_pivot_first_20160628(int[] nums, int low, int high) {
int pivot = nums[low];
int i = high+1;
for(int j = high; j > low; j--){
if(nums[j] > pivot){
i--;
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
int temp = nums[low];
nums[low] = nums[i-1];
nums[i-1] = temp;
return i-1;
}
int[] quickSort_pivot_median_20160628(int[] nums){
quickSort_pivot_median_20160628(nums, 0, nums.length-1);
return nums;
}
void quickSort_pivot_median_20160628(int[] nums, int low, int high) {
if(nums == null || nums.length == 0) return ;
if(low >= high) return;
int i = low, j = high, mid = (low+high)/2, pivot = nums[mid];
while(i <= j){
while(nums[i] < pivot) i++;
while(nums[j] > pivot) j--;
if(i <= j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
j--;
}
}
if(low < j) quickSort_pivot_median_20160628(nums, low, j);
if(i < high) quickSort_pivot_median_20160628(nums, i, high);
}
}