/**
* 快速排序
*/
public class QuickSort {
public void quickSort(int [] a,int low, int high){
if (low<high){
int middle = getMiddle(a,low,high);
quickSort(a,low,middle-1);
quickSort(a,middle+1,high);
}
}
public int getMiddle(int [] a,int low, int high){
int tmp = a[low];
while(low<high){
while(low<high&&tmp<=a[high]){
high--;
}
a[low] = a[high];
while (low<high&&tmp>=a[low]){
low++;
}
a[high] = a[low];
}
a[low] = tmp;
return low;
}
public static void main(String[] args) {
QuickSort q = new QuickSort();
int[] a ={55,33,11,8,333,52,88,99,66,0,4,8};
if(a.length>0){
q.quickSort(a,0,a.length-1);
}
System.out.println(Arrays.toString(a));
}
}
快速排序
最新推荐文章于 2021-09-09 20:08:40 发布