-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
56 lines (50 loc) · 1.07 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
// Single Pivot QuickSort
public class QuickSort
{
public static void sort(int[] arr) {qSort(arr,0,arr.length-1,false);}
public static void sort(int[] arr, boolean rev) {qSort(arr,0,arr.length-1,rev);}
private static void qSort(int[] arr, int start, int end, boolean rev)
{
if(start < end)
{
int index = partition(arr, start, end, rev);
qSort(arr,start,index-1,rev); // Sort left side
qSort(arr,index+1,end,rev); // Sort right side
}
}
private static int partition(int[] arr, int low, int high, boolean reverse)
{
int mid = low + (high-low)/2, val = arr[mid], index = low-1;
arr[mid] = arr[high];
arr[high] = val;
if(reverse)
{
for(int i = low; i < high; i++)
{
if(arr[i] > val)
{
index++;
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
else
{
for(int i = low; i < high; i++)
{
if(arr[i] < val)
{
index++;
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
arr[high] = arr[index+1];
arr[index+1] = val;
return index+1;
}
}