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

PHP implements quick sort algorithm

WBOY
Release: 2016-07-23 08:54:53
Original
902 people have browsed it
  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


Related labels:
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