1、时间复杂度
平均:O(nlogn)
最好:O(nlogn)
最差:O(n^2)
2、空间复杂度
O(logn)
3、java代码
public class Sort {
public static void quickSort(int[] arr,int left, int right){
if (left<right){
int mid = position(arr,left,right);
quickSort(arr,left,mid-1);
quickSort(arr,mid+1,right);
}
}
public static int position(int[] arr, int left, int right){
int pivot = left;
int index = pivot+1;
for (int i=index;i<=right;i++){
if (arr[i]<arr[pivot]){
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
index++;
}
}
int temp = arr[pivot];
arr[pivot] = arr[index-1];
arr[index-1] = temp;
return index-1;
}
public static void main(String[] args) {
int[] list = {8,2,9,4,3,18,21,10};
quickSort(list,0,list.length-1);
for (int j : list) {
System.out.println(j);
}
}
}