-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquickSort.java
More file actions
executable file
·62 lines (47 loc) · 1.42 KB
/
quickSort.java
File metadata and controls
executable file
·62 lines (47 loc) · 1.42 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
package quicksort;
public class quickSort {
private int array[];
private int length;
public void sort(int[] inputArr) {
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[])
{
quickSort sorter = new quickSort();
int[] input = {5,8,10,1,4,2,7,15,11,9};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}