Home > Backend Development > PHP Tutorial > PHP implements quick sort algorithm_PHP tutorial

PHP implements quick sort algorithm_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:44:52
Original
894 people have browsed it

function quicksort($seq) {

  if (count($seq) > 1) {

    $k = $seq[0];

    $x = array();

    $y = array();

    for ($i=1; $i

      if ($seq[$i] <= $k) {

        $x[] = $seq[$i];

      } else {

        $y[] = $seq[$i];

      }

    }

    $x = quicksort($x);

    $y = quicksort($y);

    return array_merge($x, array($k), $y);

  } else {

    return $seq;

  }

}

 

$arr = array(12,2,16,30,8,28,4,10,20,6,18);

print_r(quicksort($arr));

?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478709.htmlTechArticle?php function quicksort($seq) { if (count($seq) 1) { $k = $seq[0]; $x = array(); $y = array(); for ($i=1; $icount($seq); $i++) { if ($seq[$i] = $k) { $x[] = $seq[$i]; } else { $y[]...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template