-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
83 lines (76 loc) · 2.13 KB
/
QuickSort.java
File metadata and controls
83 lines (76 loc) · 2.13 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
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
package algorithm.sorting;
import java.util.Random;
/**
* The type Quick sort.
*/
public class QuickSort {
/**
* The Obj.
*/
static Helper obj = new Helper();
/**
* Sort.
*
* @param arr the arr
* @param left the left
* @param right the right
*/
public static void sort(int[] arr , int left , int right){
if(left<right){
int pi = partition(arr,left,right);
sort(arr, left, pi - 1);
sort(arr, pi + 1, right);
}
}
/**
* Partition int.
*
* @param arr the arr
* @param left the left
* @param right the right
* @return the int
*/
public static int partition(int[] arr, int left, int right){
int pivotInd = choosePivot(left,right);
obj.swap(arr, right, pivotInd);
int pivot = arr[right]; // Pivot
int i = (left - 1); // All the elements less than or equal to the
// pivot go before or at i
for (int j = left; j <= right - 1; j++) {
if (arr[j] <= pivot) {
i++; // increment the index
obj.swap(arr, i, j);
}
}
obj.swap(arr, i + 1, right); // Putting the pivot back in place
return (i + 1);
}
/**
* Choose pivot int.
*
* @param left the left
* @param right the right
* @return the int
*/
public static int choosePivot(int left, int right) {
Random rand = new Random();
// Pick 3 random numbers within the range of the array
int i1 = left + (rand.nextInt(right - left + 1));
int i2 = left + (rand.nextInt(right - left + 1));
int i3 = left + (rand.nextInt(right - left + 1));
// Return their median
return Math.max(Math.min(i1, i2), Math.min(Math.max(i1, i2), i3));
}
/**
* Main.
*
* @param args the args
*/
public static void main(String[] args){
int[] array = {2,5,1,7,9,45,0,12,69,-30,100,24,-9};
int arrSize=array.length;
obj.printArray(array, arrSize);
sort(array,0,arrSize-1);
obj.printArray(array, arrSize);
}
}