【前言】
快速排序的精髓在于每次都将某一部分按照某个pivot的大小排列,
譬如:
一个数组:
4 7 15 9 6 8 5 7 6 1 11 3 10
我们按照数组index=7的元素即【7】来划分左右两边的数组,左边数组小于7,右边数组大于7,然后可以得到:
每次交换后的结果:
交换结果:4 6 15 9 7 8 5 7 6 1 11 3 10
交换结果:4 6 5 9 7 8 15 7 6 1 11 3 10
交换结果:4 6 5 6 7 8 15 7 9 1 11 3 10
交换结果:4 6 5 6 1 8 15 7 9 7 11 3 10
交换结果:4 6 5 6 1 3 15 7 9 7 11 8 10
当前循环结果:
4 6 5 6 1 3 7 15 9 7 11 8 10
那么下面将数组分成两部分再来排序,分别为:
3---6及7到最后,如此递归,
详情可以看
下面我将自己写写改改的程序拿出来,见笑了,顺便贴出整个交换过程:
package TestCase.algorithm;
public class QuickSort {
private int _debugLevel=0;
public static void main(String[] args){
int[] _toBeSorted=new int[]{9,8,7,6,14,15,1,1,1,8,5,7,9,8,5,4,3,2,1};
QuickSort _qsort=new QuickSort(_toBeSorted) ;
_qsort.doQuickSort();
//--
//得到结果
System.out.println();
System.out.println("最后结果=============================");
_qsort.debugPrint();
}
private int[] _originArr;
public QuickSort(int[] originArray){
_originArr=originArray;
}
public int[] doQuickSort(){
if(_originArr==null||_originArr.length<=0){
return _originArr;
}
//--
System.out.println("需要排序的数组:");
debugPrint();
System.out.println("");
doPartion( (int)Math.ceil((double)_originArr.length/2),0,_originArr.length-1);
return _originArr;
}
public void doPartion(int pivotLocation,int leftLocation,int rightLocation){
int _pivot=_originArr[pivotLocation];
if(leftLocation>=rightLocation){
return;
}
//--假如是两个参数,直接比较。
System.out.println("【主元位置:“"+pivotLocation+"---"+_pivot+"”数组左界:“"+leftLocation+"”,数组右界“"+rightLocation+"”】");
if(leftLocation+1==rightLocation){
if(_originArr[leftLocation]>_originArr[rightLocation]){
int thetmp1=_originArr[leftLocation] ;
_originArr[rightLocation]=thetmp1;
_originArr[leftLocation]=thetmp1;
debugPrint(leftLocation,rightLocation);
return;
}
return;
}
int less_loc=leftLocation;//边界,少于主元的位置
int more_loc=-1;//边界,大于主元的位置
for(int cindex=leftLocation;cindex<=rightLocation;cindex++){
if(_originArr[cindex]<_pivot){
if(more_loc!=-1){
//--交换
int tmp1= _originArr[less_loc];
_originArr[less_loc]=_originArr[cindex] ;
_originArr[cindex]=tmp1;
more_loc=cindex;
System.out.print("交换结果:");
debugPrint(leftLocation,rightLocation);
System.out.println();
}
less_loc++;
}
else if(_originArr[cindex]>=_pivot){
more_loc=cindex;
}
}
//--假如less——loc-1=leftLocation,那么实际上该pivot很不幸,是最小的。
if(less_loc==leftLocation){
int tmp1=_originArr[leftLocation];
_originArr[leftLocation]=_pivot;
_originArr[pivotLocation]=tmp1;
less_loc=leftLocation+1;
}
//假如最后的less_loc小于pivot节点,那么需要交换。
else if(less_loc<pivotLocation){
_originArr[pivotLocation]=_originArr[less_loc];
_originArr[less_loc]=_pivot;
}
//--
System.out.println("当前循环结果:");
debugPrint(leftLocation,rightLocation);
System.out.println("");
if(less_loc>1){
//--左边的递归
int newRightLocation=less_loc-1;
int middlePivotLoc= (int) Math.ceil((float)(newRightLocation+leftLocation)/2);
doPartion(middlePivotLoc,leftLocation,newRightLocation);
}
if(less_loc-2<rightLocation){
int newLeftLocation=less_loc;
int newMPivotLoc=(int)Math.ceil((float)(newLeftLocation+rightLocation)/2);
doPartion(newMPivotLoc,newLeftLocation,rightLocation);
}
}
public void debugPrint(){
for (int ii:_originArr){
System.out.print(""+ii+" ");
}
}
public void debugPrint(int left,int right){
for(int ii=left;ii<=right;ii++){
System.out.print(_originArr[ii]+" ");
}
}
}
下面是这个程序的运行结果:
需要排序的数组:
9 8 7 6 14 15 1 1 1 8 5 7 9 8 5 4 3 2 1
【主元位置:“10---5”数组左界:“0”,数组右界“18”】
交换结果:1 8 7 6 14 15 9 1 1 8 5 7 9 8 5 4 3 2 1
交换结果:1 1 7 6 14 15 9 8 1 8 5 7 9 8 5 4 3 2 1
交换结果:1 1 1 6 14 15 9 8 7 8 5 7 9 8 5 4 3 2 1
交换结果:1 1 1 4 14 15 9 8 7 8 5 7 9 8 5 6 3 2 1
交换结果:1 1 1 4 3 15 9 8 7 8 5 7 9 8 5 6 14 2 1
交换结果:1 1 1 4 3 2 9 8 7 8 5 7 9 8 5 6 14 15 1
交换结果:1 1 1 4 3 2 1 8 7 8 5 7 9 8 5 6 14 15 9
当前循环结果:
1 1 1 4 3 2 1 5 7 8 8 7 9 8 5 6 14 15 9
【主元位置:“3---4”数组左界:“0”,数组右界“6”】
交换结果:1 1 1 3 4 2 1
交换结果:1 1 1 3 2 4 1
交换结果:1 1 1 3 2 1 4
当前循环结果:
1 1 1 3 2 1 4
【主元位置:“3---3”数组左界:“0”,数组右界“5”】
交换结果:1 1 1 2 3 1
交换结果:1 1 1 2 1 3
当前循环结果:
1 1 1 2 1 3
【主元位置:“2---1”数组左界:“0”,数组右界“4”】
当前循环结果:
1 1 1 2 1
【主元位置:“3---2”数组左界:“1”,数组右界“4”】
交换结果:1 1 1 2
当前循环结果:
1 1 1 2
【主元位置:“2---1”数组左界:“1”,数组右界“3”】
当前循环结果:
1 1 1
【主元位置:“3---1”数组左界:“2”,数组右界“3”】
【主元位置:“13---8”数组左界:“7”,数组右界“18”】
交换结果:5 7 7 8 8 9 8 5 6 14 15 9
交换结果:5 7 7 5 8 9 8 8 6 14 15 9
交换结果:5 7 7 5 6 9 8 8 8 14 15 9
当前循环结果:
5 7 7 5 6 8 9 8 8 14 15 9
【主元位置:“9---7”数组左界:“7”,数组右界“11”】
交换结果:5 5 7 7 6
交换结果:5 5 6 7 7
当前循环结果:
5 5 6 7 7
【主元位置:“8---5”数组左界:“7”,数组右界“9”】
当前循环结果:
5 5 6
【主元位置:“9---6”数组左界:“8”,数组右界“9”】
【主元位置:“11---7”数组左界:“10”,数组右界“11”】
【主元位置:“15---8”数组左界:“12”,数组右界“18”】
当前循环结果:
8 9 8 8 14 15 9
【主元位置:“16---14”数组左界:“13”,数组右界“18”】
交换结果:9 8 8 9 15 14
当前循环结果:
9 8 8 9 15 14
【主元位置:“15---8”数组左界:“13”,数组右界“16”】
当前循环结果:
8 8 9 9
【主元位置:“15---9”数组左界:“14”,数组右界“16”】
当前循环结果:
8 9 9
【主元位置:“16---9”数组左界:“15”,数组右界“16”】
【主元位置:“18---14”数组左界:“17”,数组右界“18”】
15 15
最后结果=============================
1 1 1 1 2 3 4 5 5 6 7 7 8 8 8 9 9 15 15
看,虽然完全没有高手的风格,但是还是可以自娱自乐。