-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
60 lines (49 loc) · 1.02 KB
/
QuickSort.java
File metadata and controls
60 lines (49 loc) · 1.02 KB
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
public class QuickSort {
public static int pivotPos(int[] input,int beg,int end){
int count=0;
int t=beg+1;
int l=end;
while(t<=end)
{
if(input[beg]>=input[t]){
count++;
}
t++;
}
t=beg;
int temp=input[beg];
input[beg]=input[count+beg];
input[count+beg]=temp;
while(t<l){
if(input[t]<=input[count+beg]){
t++;
}
if(input[l]>=input[count+beg]){
l--;
}
if(input[t]>input[count+beg]&&input[l]<input[count+beg]){
temp=input[t];
input[t]=input[l];
input[l]=temp;
t++;
l--;
}
}
return beg+count;
}
public static void quickSort(int[] input,int beg,int end){
if(beg>=end){
return;
}
int PivotPos=pivotPos(input,beg,end);
quickSort(input,beg,PivotPos-1);
quickSort(input,PivotPos+1,end);
}
public static void main(String[] args) {
int[] input={6,6,5,4,1};
quickSort(input,0,input.length-1);
for(int i=0;i<input.length;i++){
System.out.println(input[i]);
}
}
}