-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.java
More file actions
67 lines (63 loc) · 1002 Bytes
/
Copy pathQuicksort.java
File metadata and controls
67 lines (63 loc) · 1002 Bytes
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
package quicksort;
public class Quicksort {
public int[] A;
public int n;
Quicksort(int[] A,int n){
this.A=A;
this.n=n;
}
public void display(){
for(int i=0; i<n; i++){
System.out.print(A[i]+" ");
}
System.out.println();
}
public void swap(int i,int j){
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
public Range find_partition(int start,int end){
int j=start+1,k=end;
int pivot = A[start];
int eq_count=0;
int begin=start+1;
while(j<=k){
while( j<=k && A[j]<=pivot ){
if(A[j]==pivot){
eq_count++;
swap(j,begin);
begin++;
}
j++;
}
while(A[k]>pivot)
k--;
if(j<=k)
{
swap(j,k);
j++;
k--;
}
}
if(k>=start)
swap(start,k);
int s=k;
begin=start+1;
for(s=k;eq_count>0; eq_count--){
s--;
swap(begin,s);
begin++;
}
return new Range(s,k);
}
public void Quicksort(int start,int end){
if(start==end)
return;
Range r=find_partition(start, end);
if(start<(r.q-1))
Quicksort(start,r.q-1);
if(end>r.t+1)
Quicksort(r.t+1,end);
}
}