【算法总结】快速排序算法

【算法解释】

快速排序的核心思想是:(这里排序是从小到大----从大到小也是类似的)

找出一个pivot(关键点),然后以关键点为标准,对数组各个位置进行比较,让关键点左边的数字都少于或等于关键点,关键点右边的都大于或等于关键点,

然后划分成为两半---关键点左边(不包含关键点)数组及关键点右边(不包含关键点),递归应用划分两半的过程,到了数组个数为1或2的时候就结束。

这个过程就是快速排序。

【下面对相关过程进行说明】



譬如:

一个数组:

[java]  view plain copy
  1. 4  7  15  9  6  8  5  7  6  1  11  3  10    

我们按照数组index=7的元素即【7】来划分左右两边的数组,左边数组小于7,右边数组大于7,然后可以得到:

每次交换后的结果:

[java]  view plain copy
  1. 交换结果:4   6   15   9   7   8   5   7   6   1   11   3   10     
  2. 交换结果:4   6   5   9   7   8   15   7   6   1   11   3   10     
  3. 交换结果:4   6   5   6   7   8   15   7   9   1   11   3   10     
  4. 交换结果:4   6   5   6   1   8   15   7   9   7   11   3   10     
  5. 交换结果:4   6   5   6   1   3   15   7   9   7   11   8   10     
  6. 当前循环结果:  
  7. 4   6   5   6   1   3   7   15   9   7   11   8   10     

那么下面将数组分成两部分再来排序,分别为:

3---6及7到最后,如此递归,

详情可以看

JULY的快速排序算法解释。


下面我将自己写写改改的程序拿出来,见笑了,顺便贴出整个交换过程:

[java]  view plain copy
  1. package TestCase.algorithm;  
  2.   
  3. public class QuickSort {  
  4.     private int _debugLevel=0;  
  5.   
  6.     public static void main(String[] args){  
  7.     int[] _toBeSorted=new int[]{9,8,7,6,14,15,1,1,1,8,5,7,9,8,5,4,3,2,1};  
  8.         QuickSort _qsort=new QuickSort(_toBeSorted) ;  
  9.         _qsort.doQuickSort();  
  10.         //--  
  11.         //得到结果  
  12.         System.out.println();  
  13.         System.out.println("最后结果=============================");  
  14.         _qsort.debugPrint();  
  15.   
  16.   
  17.     }  
  18.   
  19.     private int[] _originArr;  
  20.     public QuickSort(int[] originArray){  
  21.         _originArr=originArray;  
  22.     }  
  23.   
  24.     public int[] doQuickSort(){  
  25.   
  26.   
  27.         if(_originArr==null||_originArr.length<=0){  
  28.             return _originArr;  
  29.         }  
  30.         //--  
  31.         System.out.println("需要排序的数组:");  
  32.         debugPrint();  
  33.         System.out.println("");  
  34.        doPartion( (int)Math.ceil((double)_originArr.length/2),0,_originArr.length-1);  
  35.   
  36.   
  37.         return _originArr;  
  38.     }  
  39.   
  40.     public void doPartion(int pivotLocation,int leftLocation,int rightLocation){  
  41.   
  42.         int _pivot=_originArr[pivotLocation];  
  43.         if(leftLocation>=rightLocation){  
  44.   
  45.             return;  
  46.         }  
  47.         //--假如是两个参数,直接比较。  
  48.         System.out.println("【主元位置:“"+pivotLocation+"---"+_pivot+"”数组左界:“"+leftLocation+"”,数组右界“"+rightLocation+"”】");  
  49.         if(leftLocation+1==rightLocation){  
  50.             if(_originArr[leftLocation]>_originArr[rightLocation]){  
  51.                 int thetmp1=_originArr[leftLocation] ;  
  52.                 _originArr[rightLocation]=thetmp1;  
  53.                 _originArr[leftLocation]=thetmp1;  
  54.   
  55.                 debugPrint(leftLocation,rightLocation);  
  56.                 return;  
  57.             }  
  58.             return;  
  59.         }  
  60.   
  61.   
  62.         int less_loc=leftLocation;//边界,少于主元的位置  
  63.         int more_loc=-1;//边界,大于主元的位置  
  64.   
  65.         for(int cindex=leftLocation;cindex<=rightLocation;cindex++){  
  66.   
  67.   
  68.   
  69.             if(_originArr[cindex]<_pivot){  
  70.   
  71.   
  72.                 if(more_loc!=-1){  
  73.                     //--交换  
  74.                     int tmp1= _originArr[less_loc];  
  75.                     _originArr[less_loc]=_originArr[cindex] ;  
  76.                     _originArr[cindex]=tmp1;  
  77.                      more_loc=cindex;  
  78.   
  79.   
  80.                     System.out.print("交换结果:");  
  81.                     debugPrint(leftLocation,rightLocation);  
  82.                     System.out.println();  
  83.                 }  
  84.                   less_loc++;  
  85.   
  86.   
  87.   
  88.             }  
  89.             else if(_originArr[cindex]>=_pivot){  
  90.                 more_loc=cindex;  
  91.             }  
  92.   
  93.   
  94.   
  95.   
  96.   
  97.   
  98.   
  99.         }  
  100.   
  101.         //--假如less——loc-1=leftLocation,那么实际上该pivot很不幸,是最小的。  
  102.         if(less_loc==leftLocation){  
  103.             int tmp1=_originArr[leftLocation];  
  104.             _originArr[leftLocation]=_pivot;  
  105.             _originArr[pivotLocation]=tmp1;  
  106.             less_loc=leftLocation+1;  
  107.         }  
  108.         //假如最后的less_loc小于pivot节点,那么需要交换。  
  109.         else if(less_loc<pivotLocation){  
  110.             _originArr[pivotLocation]=_originArr[less_loc];  
  111.             _originArr[less_loc]=_pivot;  
  112.   
  113.         }  
  114.         //--  
  115.         System.out.println("当前循环结果:");  
  116.         debugPrint(leftLocation,rightLocation);  
  117.         System.out.println("");  
  118.   
  119.         if(less_loc>1){  
  120.             //--左边的递归  
  121.             int newRightLocation=less_loc-1;  
  122.               int middlePivotLoc=   (int) Math.ceil((float)(newRightLocation+leftLocation)/2);  
  123.             doPartion(middlePivotLoc,leftLocation,newRightLocation);  
  124.   
  125.         }  
  126.   
  127.         if(less_loc-2<rightLocation){  
  128.             int newLeftLocation=less_loc;  
  129.            int newMPivotLoc=(int)Math.ceil((float)(newLeftLocation+rightLocation)/2);  
  130.             doPartion(newMPivotLoc,newLeftLocation,rightLocation);  
  131.   
  132.   
  133.         }  
  134.   
  135.   
  136.     }  
  137.   
  138.     public void debugPrint(){  
  139.         for (int ii:_originArr){  
  140.             System.out.print(""+ii+"  ");  
  141.         }  
  142.   
  143.     }  
  144.   
  145.     public  void debugPrint(int left,int right){  
  146.         for(int ii=left;ii<=right;ii++){  
  147.             System.out.print(_originArr[ii]+"   ");  
  148.         }  
  149.   
  150.     }  
  151.   
  152. }  
下面是这个程序的运行结果:

[java]  view plain copy
  1. 需要排序的数组:  
  2. 9  8  7  6  14  15  1  1  1  8  5  7  9  8  5  4  3  2  1    
  3. 【主元位置:“10---5”数组左界:“0”,数组右界“18”】  
  4. 交换结果:1   8   7   6   14   15   9   1   1   8   5   7   9   8   5   4   3   2   1     
  5. 交换结果:1   1   7   6   14   15   9   8   1   8   5   7   9   8   5   4   3   2   1     
  6. 交换结果:1   1   1   6   14   15   9   8   7   8   5   7   9   8   5   4   3   2   1     
  7. 交换结果:1   1   1   4   14   15   9   8   7   8   5   7   9   8   5   6   3   2   1     
  8. 交换结果:1   1   1   4   3   15   9   8   7   8   5   7   9   8   5   6   14   2   1     
  9. 交换结果:1   1   1   4   3   2   9   8   7   8   5   7   9   8   5   6   14   15   1     
  10. 交换结果:1   1   1   4   3   2   1   8   7   8   5   7   9   8   5   6   14   15   9     
  11. 当前循环结果:  
  12. 1   1   1   4   3   2   1   5   7   8   8   7   9   8   5   6   14   15   9     
  13. 【主元位置:“3---4”数组左界:“0”,数组右界“6”】  
  14. 交换结果:1   1   1   3   4   2   1     
  15. 交换结果:1   1   1   3   2   4   1     
  16. 交换结果:1   1   1   3   2   1   4     
  17. 当前循环结果:  
  18. 1   1   1   3   2   1   4     
  19. 【主元位置:“3---3”数组左界:“0”,数组右界“5”】  
  20. 交换结果:1   1   1   2   3   1     
  21. 交换结果:1   1   1   2   1   3     
  22. 当前循环结果:  
  23. 1   1   1   2   1   3     
  24. 【主元位置:“2---1”数组左界:“0”,数组右界“4”】  
  25. 当前循环结果:  
  26. 1   1   1   2   1     
  27. 【主元位置:“3---2”数组左界:“1”,数组右界“4”】  
  28. 交换结果:1   1   1   2     
  29. 当前循环结果:  
  30. 1   1   1   2     
  31. 【主元位置:“2---1”数组左界:“1”,数组右界“3”】  
  32. 当前循环结果:  
  33. 1   1   1     
  34. 【主元位置:“3---1”数组左界:“2”,数组右界“3”】  
  35. 【主元位置:“13---8”数组左界:“7”,数组右界“18”】  
  36. 交换结果:5   7   7   8   8   9   8   5   6   14   15   9     
  37. 交换结果:5   7   7   5   8   9   8   8   6   14   15   9     
  38. 交换结果:5   7   7   5   6   9   8   8   8   14   15   9     
  39. 当前循环结果:  
  40. 5   7   7   5   6   8   9   8   8   14   15   9     
  41. 【主元位置:“9---7”数组左界:“7”,数组右界“11”】  
  42. 交换结果:5   5   7   7   6     
  43. 交换结果:5   5   6   7   7     
  44. 当前循环结果:  
  45. 5   5   6   7   7     
  46. 【主元位置:“8---5”数组左界:“7”,数组右界“9”】  
  47. 当前循环结果:  
  48. 5   5   6     
  49. 【主元位置:“9---6”数组左界:“8”,数组右界“9”】  
  50. 【主元位置:“11---7”数组左界:“10”,数组右界“11”】  
  51. 【主元位置:“15---8”数组左界:“12”,数组右界“18”】  
  52. 当前循环结果:  
  53. 8   9   8   8   14   15   9     
  54. 【主元位置:“16---14”数组左界:“13”,数组右界“18”】  
  55. 交换结果:9   8   8   9   15   14     
  56. 当前循环结果:  
  57. 9   8   8   9   15   14     
  58. 【主元位置:“15---8”数组左界:“13”,数组右界“16”】  
  59. 当前循环结果:  
  60. 8   8   9   9     
  61. 【主元位置:“15---9”数组左界:“14”,数组右界“16”】  
  62. 当前循环结果:  
  63. 8   9   9     
  64. 【主元位置:“16---9”数组左界:“15”,数组右界“16”】  
  65. 【主元位置:“18---14”数组左界:“17”,数组右界“18”】  
  66. 15   15     
  67. 最后结果=============================  
  68. 1  1  1  1  2  3  4  5  5  6  7  7  8  8  8  9  9  15  15    

看,虽然完全没有高手的风格,但是还是可以自娱自乐。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值