-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort
More file actions
34 lines (30 loc) · 1012 Bytes
/
quickSort
File metadata and controls
34 lines (30 loc) · 1012 Bytes
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
class Result {
static void swape(List<Integer> arr,int num1,int num2){
int tem=arr.get(num1);
arr.set(num1, arr.get(num2));
arr.set(num2, tem);
}
static void quickSortRecursive(List<Integer> arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSortRecursive(arr, low, pi - 1);
quickSortRecursive(arr, pi + 1, high);
}
}
static int partition(List<Integer> arr,int low,int high){
int pivot= low;
while(low<high){
while(arr.get(low)<=arr.get(pivot) && low<high) low++;
while(arr.get(high)>arr.get(pivot)) high--;
if(low<high){
swape(arr, low, high);
}
}
swape(arr,high,pivot);
return high;
}
public static List<Integer> quickSort(List<Integer> arr) {
quickSortRecursive(arr,0,arr.size()-1);
return arr;
}
}