-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
86 lines (73 loc) · 1.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class QuickSort {
public static void main(String[] args) {
int[] integers = {10,6,12, 4, 100, 2, 18,9, 17, 5};
quickSort(integers, 0, integers.length-1);
//int rank = partition1(integers, 0, integers.length-1);
//System.out.println(rank);
for(int i : integers){
System.out.println(i);
}
}
private static void quickSort(int[] array , int lo, int hi){
if(lo<hi){
int pi = partition(array, lo, hi);
quickSort(array, lo, pi-1);
quickSort(array, pi, hi);
}
}
private static int partition1(int[] array , int lo, int hi){
int i = lo-1;
int pivot = array[hi];
/*
* j=0 10,6,12, 4, 100, 2, 18,9
* j=1 6,10,12,4,100,2,18,9 , i=0
* j=2 6,10,12,4,100,2,18,9
* j=3 6,4,12,10,100,2,18,9 , i=1
* j=4 6,4,12,10,100,2,18,9
* j=5 6,4,2,10,100,12,18,9 , i=2
* j=6 6,4,2,10,100,12,18,9 , i=2
* j =7 will not come as j<hi which is j<7
*
*/
for(int j=lo; j<hi ; j++){
if(array[j]<=pivot){
i++;
//System.out.println(i+" = "+j);
//swap(array, i , j);
}
}
//System.out.println(i+" - "+hi);
swap(array, i+1, hi);
return i+1;
}
private static int partition(int[] array , int lo, int hi){
int i = lo-1;
int pivot = array[hi];
/*
* j=0 10,6,12, 4, 100, 2, 18,9
* j=1 6,10,12,4,100,2,18,9 , i=0
* j=2 6,10,12,4,100,2,18,9
* j=3 6,4,12,10,100,2,18,9 , i=1
* j=4 6,4,12,10,100,2,18,9
* j=5 6,4,2,10,100,12,18,9 , i=2
* j=6 6,4,2,10,100,12,18,9 , i=2
* j =7 will not come as j<hi which is j<7
*
*/
for(int j=lo; j<hi ; j++){
if(array[j]<=pivot){
i++;
//System.out.println(i+" = "+j);
swap(array, i , j);
}
}
//System.out.println(i+" - "+hi);
swap(array, i+1, hi);
return i+1;
}
private static void swap(int array[], int index1, int index2){
int t = array[index1];
array[index1] = array[index2] ;
array[index2] = t;
}
}