快速排序 理想状态O(n log2n) 最差O(n2), 取决于随机数的选择,是否能将序列平均划分
public class QuickSort {
public int[] quickSort(int[] A, int n) {
// write code here
doSort(A, 0, n-1);
return A;
}
private void doSort(int[] A ,int low, int high){
if(low<high){
int position = parition(A, low, high);
doSort(A, low, position-1);
doSort(A, position+1, high);
}
}
private int parition(int[] A, int low, int high){
int pivot = A[low];
while(low<high){
while(low<high&&A[high]>=pivot) high--;
A[low] = A[high];
while(low<high&&A[low]<=pivot) low++;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
}