Home > Backend Development > PHP Tutorial > PHP implements quick sorting of arrays using iteration

PHP implements quick sorting of arrays using iteration

WBOY
Release: 2016-07-25 08:45:27
Original
901 people have browsed it
  1. function quicksortX(&$seq)
  2. {
  3. $stack = array($seq);
  4. $sort = array();
  5. while ($stack) {
  6. $arr = array_pop($stack);
  7. if(count($arr) <= 1){
  8. if (count($arr) == 1) {
  9. $sort[] = &$arr[0];
  10. }
  11. continue;
  12. }
  13. $k = $arr[0];
  14. $x = array();
  15. $y = array();
  16. $_size = count($arr);
  17. for ($i = 1; $i < $_size; $i++) {
  18. if ($arr[$i] <= $k) {
  19. $x[] = &$arr[$i];
  20. } else {
  21. $y[] = &$arr[$i];
  22. }
  23. }
  24. !empty($y) && array_push($stack, $y);
  25. array_push($stack, array($arr[0]));
  26. !empty($x) && array_push($stack, $x);
  27. }
  28. return $sort;
  29. }
复制代码

迭代, PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template