- Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathQuickSortExample.java
More file actions
Latest commit
37 lines (33 loc) · 954 Bytes
/
Copy pathQuickSortExample.java
File metadata and controls
37 lines (33 loc) · 954 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
35
36
37
packagech17;
importjava.util.Arrays;
publicclassQuickSortExample {
staticintpartition(int[] A, intlo, inthi) {
intpivot = A[hi];
intleft = lo;
for (intright = lo; right < hi; right++) {
if (A[right] < pivot) {
inttemp = A[left];
A[left] = A[right];
A[right] = temp;
left++;
}
}
inttemp = A[left];
A[left] = A[hi];
A[hi] = temp;
returnleft;
}
staticint[] Quicksort(int[] A, intlo, inthi) {
if (lo < hi) {
intpivot = partition(A, lo, hi);
Quicksort(A, lo, pivot - 1);
Quicksort(A, pivot + 1, hi);
}
returnA;
}
publicstaticvoidmain(String[] args) {
int[] A = newint[]{38, 27, 43, 3, 9, 82, 10};
Quicksort(A, 0, A.length - 1);
System.out.println(Arrays.toString(A));
}
}