- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathQuickSortArray.java
44 lines (37 loc) · 1.05 KB
/
QuickSortArray.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
/* Program to Sort an Array Using the QuickSort Sorting Algorithm */
publicclassQuickSortArray {
publicstaticvoidmain(String[] args) {
intarr[] = {1000,6,3,5,2,8,9,12,200};
intn = arr.length;
quickSort(arr, 0, n-1);
for (inti = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
publicstaticvoidquickSort(intarr[], intlow, inthigh) {
if(low < high)
{
intpindex = partition(arr, low, high);
quickSort(arr, low, pindex-1);
quickSort(arr, pindex+1, high);
}
}
publicstaticintpartition(intarr[], intlow, inthigh) {
intpiv = arr[high];
inti=low-1;
for (intj = low; j < high; j++) {
if(arr[j]<piv)
{
i++;
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
inttemp = arr[i];
arr[i] = piv;
arr[high] = temp;
returni;
}
}