切分:
切分
public class QuickSort {
private static int partition(int[] a, int l, int h) {
int i = l;
int j = h + 1;
int k = a[l];
while (true) {
while (k > a[++i]) {
if (i == h) break;
}
while (k < a[--j]) {
if (j == l) break;
}
if (i >= j) break;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
a[l] = a[j];
a[j] = k;
return j;
}
private static void quickSort(int[] a,int l,int h){
if(l>= h) return;
int p = partition(a,l,h);
quickSort(a,l,p-1);
quickSort(a,p+1,h);
}
public static void main(String[] args){
int[] a = {3,43,70,97,98,7,545,8,6,78,1,20,98,6,76};
quickSort(a,0,a.length-1);
for(int i:a){
System.out.print(i+", ");
}
}
}
网友评论