快速排序,真的很快
快速排序和 归并排序的速度都很快, 因为它们都是在不断地 缩小问题的规模
/**
* Created by fupeng on 2017/1/20.
*/
public class quick {
static void show(int [] a){
for(int i : a){
System.out.println(i);
}
}
static void exch(int [] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static void quick(int []a, int low, int high){
if(low >= high){
return;
}
int key = a[low];
int i=low;
int j=high;
while(i!=j){
while(a[j]>=key &&i<j)
j--;
while(a[i] <= key && i<j)
i++;
if(i<j)
exch(a,i,j);
}
exch(a,i,low);
quick(a,low, i-1);
quick(a,i+1, high);
}
static void sort(int [] a){
quick(a, 0, a.length-1);
}
public static void main(String[] args) {
int [] a = {2,3,4,1,5,9,8,6,7,0};
sort(a);
show(a);
}
}