-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickdivide.java
48 lines (37 loc) · 1.15 KB
/
quickdivide.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
class quickdivide {
static int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return (i + 1);
}
static void quickSort(int array[], int low, int high) {
if (low < high) {
int search = partition(array, low, high);
quickSort(array, low, search - 1);
quickSort(array, search + 1, high);
}
}
public static void main(String[] args) {
int N = 100;
int[] arr1 = new int[N];
for (int i = N - 1; i >= 0; i--) {
arr1[i] = N - i;
}
quickSort(arr1, 0, N - 1);
System.out.println("sorted array is:");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
}
}