<?php/** * created by phpstorm. * user: saint * date: 15/ 8/5 * time: Morning 11:49 */class demo{ public $a = Array (3, 6, 9, 2, 4, 7, 1, 5, 8, 0); Public function qsort ($left, $right) { if ($left > $right) { return; } $i = $left; $j = $right; $standard = $this->a[$left]; while ($i != $j) { // Find cells while from right to left that are smaller than the base number ($standard <= $this->a[$j]) && ($j > $i)) { $j--; } / / from left to right to find while (($standard >= $this->a[$i]) && ($j > $i)) { $i ++; } $tmp = $this->a[$i]; $this->a[$i] = $this->a[$j]; $this->a[$j] = $tmp; } // determine the position of the base number $this->a[$left] = $this- >a[$i]; $this->a[$i] = $standard; $this->qsort ($left, $i - 1); $this->qsort ($i + 1, $right); } // execution Function public function main () { $left = 0; $right = count ($this->a) - 1; $ This->qsort ($left, $right); print_r ($this->a); }} $demo = new demo (); $demo->main ();
A simple quick sort