当前位置:Gxlcms > PHP教程 > 快速排序Qsort

快速排序Qsort

时间:2021-07-01 10:21:17 帮助过:34人阅读

  1. <!--?php
  2. function Partition(&$arr,$low,$high)
  3. {
  4. $_t=$arr[$low];
  5. while($low<$high)
  6. {
  7. //注意第二个限制条件,不然如果一直都是大于会超过数组下标
  8. while($arr[$high]-->$_t&&$high>$low) --$high;
  9. $arr[$low]=$arr[$high];
  10. while($arr[$low]<$_t&&$low<$high) ++$low;
  11. $arr[$high]=$arr[$low];
  12. }
  13. $arr[$low]=$_t;
  14. return $low;
  15. }
  16. function Qsort(&$arr,$low,$high)
  17. {
  18. if($low<$high)
  19. {
  20. $mid=Partition($arr,$low,$high);
  21. Qsort($arr,$low,$mid-1);
  22. Qsort($arr,$mid+1,$high);
  23. }
  24. }
  25. $a=array(9,8,7,10,11,4,3,2,1);
  26. Qsort($a,0,8);
  27. var_dump($a);

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了快速排序Qsort,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行