
//有序数组直接选择第一个元素作轴效果不理想
public class NormalQuickSort {
public int partion(int[] a,int start,int end){
int pivot = a[start];
int i = start;
int j = end;
while (i < j){
while (i < j && a[j] >= pivot){
j--;
}
a[i] = a[j];
while (i < j && a[i] <= pivot){
i++;
}
a[j] = a[i];
}
a[i] = pivot;
return i;
}
public void sort(int[] a,int start,int end){
if(start < end){
int partition = partion(a,start,end);
sort(a,start,partition-1);
sort(a,partition+1,end);
}
}
@Test
public void test(){
int[] a = {6,4,3,3,1,2,9,5};
sort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
}
近期评论