Home  >  Article  >  Backend Development  >  PHP implements quick sort algorithm

PHP implements quick sort algorithm

WBOY
WBOYOriginal
2016-07-23 08:54:53877browse
  1. function quicksort($seq) {
  2. if (count($seq) > 1) {
  3. $k = $seq[0];
  4. $x = array();
  5. $y = array();
  6. for ($i=1; $i
  7. if ($seq[$i] <= $k) {
  8. $x[] = $seq[$i];
  9. } else {
  10. $y[] = $seq[$i];
  11. }
  12. }
  13. $x = quicksort($x);
  14. $y = quicksort($y);
  15. return array_merge($x, array($k), $y);
  16. } else {
  17. return $seq;
  18. }
  19. }
  20. $arr = array(12,2,16,30,8,28,4,10,20,6,18);
  21. print_r(quicksort($arr));
  22. ?>
复制代码

PHP


Statement:
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