-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.java
57 lines (46 loc) · 1.12 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
import java.util.*;
class Quicksort {
public static void main(String args[]) {
int[] arr = {
7,
9,
2,
3,
6,
4,
8,
9,
5
};
quicksort(arr,0,arr.length-1);
// System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void quicksort(int[] arr,int low,int high) {
if(low > high) {
return;
}
int partnidx = partition_index(arr,low,high);
quicksort(arr,low,partnidx-1);
quicksort(arr,partnidx+1,high);
}
public static int partition_index(int[] arr,int low,int high) {
int i = low;
int j = low;
int pivot = arr[high];
while (i <= high) {
if (arr[i] > pivot) {
i++;
} else {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j++;
}
}
return j-1;
}
}