parent
d9a8742b66
commit
15112ddb05
@ -0,0 +1,39 @@
|
||||
package sort;
|
||||
|
||||
|
||||
public class QuickSort {
|
||||
|
||||
private static void exch(int[] a,int i,int j){
|
||||
int t=a[i];
|
||||
a[i]=a[j];
|
||||
a[j]=t;
|
||||
}
|
||||
|
||||
private static int partition(int[] a,int lo,int hi){
|
||||
int i=lo,j=hi+1;
|
||||
int v=a[lo];
|
||||
while(true){
|
||||
while(a[++i]<v)
|
||||
if(i==hi)
|
||||
break;
|
||||
while(a[--j]>v)
|
||||
if(j==lo)
|
||||
break;
|
||||
if(i>=j)
|
||||
break;
|
||||
exch(a,i,j);
|
||||
}
|
||||
exch(a,lo,j);
|
||||
return j;
|
||||
}
|
||||
private static void sort(int[] a, int lo,int hi){
|
||||
if(hi<=lo)
|
||||
return;
|
||||
int j=partition(a,lo,hi);
|
||||
sort(a,lo,j-1);
|
||||
sort(a,j+1,hi);
|
||||
}
|
||||
public void sort(int[] a){
|
||||
sort(a,0,a.length-1);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue