之前看快速排序算法感觉不是很很简单易懂,根据自己理解写了一个。
public class QuickSort {
public static void main(String[] args) {
int[] array = new int[]{2,8,7,1,3,5,6,4};
// int[] array = new int[]{12,34,4,99,65,25,77,48,66,91,64,87,3};
quickSort(array,0,array.length-1);
for(int i=0;i<array.length;i++){
System.out.print(" "+array[i]);
}
}
public static void quickSort(int[] array,int left,int right){
//分界值
int boundValue;
//insertsub 作为分界值的插入一下表,分类完成后将将分界值插入该下表对应的元素
int insertsub;
//对数组截取部分由小到大遍历的数组下标。
int cursorsub;
insertsub = left;
cursorsub = left;
boundValue = array[right];
while(cursorsub<right){
if(array[cursorsub]<boundValue){
int temp = array[cursorsub];
array[cursorsub] = array[insertsub];
array[insertsub] = temp;
insertsub++;
}
cursorsub++;
}
array[right] = array[insertsub];
array[insertsub] = boundValue;
if(left<insertsub){
quickSort(array,left,insertsub-1);
}
if(right>insertsub){
quickSort(array,insertsub+1,right);
}
}
}