private static void sort(int [] a ,int start, int end) {
//只有一个数或者没有数了,就不要排序了
if (start>=end)
return ;
//第一次排序,获取排序后的基准值的位置
int i = quickSort(a, start, end);
//排序基准值左边的
sort(a,start,i-1);
//排序基准值右边的
sort(a,i+1,end);
}
private static int quickSort(int [] a ,int start, int end){
//取一个基准值,用于比较(大于他的数放在左边,否则放在右边)
int base=a[end];
//只要左右节点没有汇合,就一直遍历
while(start<end){
//从左遍历,直到有一个数大于基准值
while(start<end&&a[start]<=base)
start++;
//将大于基准值的数和几基准值互换
if(start<end){
int temp=a[start];
a[start]=a[end];
a[end]=temp;
//交换后,右边这个数就是排完了的
end--;
}
//从右遍历,直到有一个数小于基准值
while(start<end&&a[end]>=base)
end--;
//将小于基准值的数和几基准值互换
if (start<end){
int temp =a[end];
a[end]=a[start];
a[start]=temp;
//交换后,左边这个数就是排完了的
start++;
}
}
//返回基准值的位置
return start;
}