-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathQuickSort.java
86 lines (67 loc) · 1.42 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
/*
Solved By: Sandeep Ranjan (1641012352)
*/
class ArrayQuick {
private int[] arr;
private int nElems;
public ArrayQuick(int max) {
arr = new int[max];
nElems = 0;
}
public void insert(int value) {
arr[nElems] = value;
nElems++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(arr[j] + " ");
System.out.println("");
}
public void quickSort(int left, int right) {
if(right-left <= 0)
return;
else {
int pivot = arr[right];
int partition = partitionIt(left, right, pivot);
quickSort(left, partition-1); // sort left side
quickSort(partition+1, right); // sort right side
}
}
private int partitionIt(int left, int right, int pivot) {
int leftPtr = left - 1;
int rightPtr = right;
while(true) {
while( arr[++leftPtr] < pivot );
while(rightPtr > 0 && arr[--rightPtr] > pivot);
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right);
return leftPtr;
}
private void swap(int indx1, int indx2) {
int temp = arr[indx1];
arr[indx1] = arr[indx2];
arr[indx2] = temp;
}
}
class QuickSort {
public static void main(String[] args) {
int maxSize = 10;
ArrayQuick a = new ArrayQuick(maxSize);
a.insert(77);
a.insert(99);
a.insert(44);
a.insert(55);
a.insert(22);
a.insert(88);
a.insert(11);
a.insert(86);
a.insert(66);
a.insert(33);
a.quickSort(0, maxSize-1);
a.display();
}
}