平均时间复杂度是 O(nlogn);在传入的数组为倒序时,将出现最坏的情况,时间复杂度为O(n^2)
private static void myQuickSort(int[] arr, int start, int end) {
if(start>=end){
return;
}
int divideIndex = divide(arr, start, end);
//分治法,按divideIndex将数组分成两部分达到分治的效果
myQuickSort(arr,start,divideIndex-1);
myQuickSort(arr,divideIndex+1,end);
}
//返回数组中的索引,将其划分为两部分
private static int divide(int[] arr, int start, int end) {
//将开始位置作为坑,坑有divideIndex的属性
int pivot=arr[start];
int divideIndex=start;
//一轮比较的结束标志
while (start<=end){
//循环从后向前比较指定值。如果大于指定值,end--;如果小于指定值,end位置值填入坑,并作为新坑,旧坑位置比较完成(start++),跳出
while (start<=end){
if(arr[end]<pivot){
divideIndex=end;
arr[start]=arr[end];
start++;
break;
}
end--;
}
//循环从前往后比较指定值,如果小于指定值,start++;如果大于指定值,start位置值填入坑,并作为新坑,旧坑位置比较完成(end--),跳出
while (start<=end){
if(arr[start]>pivot){
divideIndex=start;
arr[end]=arr[start];
end--;
break;
}
start++;
}
}
//!!将开始取出的中心点赋值到数组的中心位置
arr[divideIndex]=pivot;
return divideIndex;
}
快排图解参考:http://www.sohu.com/a/246785807_684445
算法系列参考:https://blog.youkuaiyun.com/bigbug_500/article/category/7988556