package QuickSort;
public class QuickSort {
//时间复杂度O(nlogn) 最坏O(n2)
//空间复杂度O(logn) 最坏O(n)
public static void main(String[] args){
int[] a = {55,99,44,22,44,55,99,44,22,44,888,4
};
quicksort(a,0,a.length-1
);
for(
int i=0;i<a.length;i++
){
System.out.print(a[i]+" "
);
}
}
public static void quicksort(
int[] a,
int low,
int high){
if(low<high){
//不加这个下面分段就越界了
int middle=
getmiddle(a,low,high);
quicksort(a,middle+1
,high);
quicksort(a,0,middle-1
);
}
}
public static int getmiddle(
int[] a ,
int low,
int high){
int temp =
a[low];
while(low<
high){
while(low<high&&temp<=
a[high]){
high--
;
}
a[low]=
a[high];
while(low<high&&a[low]<=
temp){
low++
;
}
a[high]=
a[low];
}
a[low]=
temp;
return low;
}
}
转载于:https://www.cnblogs.com/cmmmmmmmc/p/5491024.html