In this paper, PHP implementation of array sorting examples, you can achieve fast sorting, heap sorting, hill sorting, insert sorting and so on. Refer to a friend who needs it. Sample PHP array, the code is as follows:
5) {while ($low < $high) {$key = Excute ($list, $low, $high), Fast ($list, $low, $key-1),//fast ($list, $key + 1, $high);// Common Recursive implementation $low = $key + 1;//tail recursive implementation}}else{insert ($list);}} The quick row performs a sort function Excute (& $list, $low, $high) {swap ($list, $low, ($low + $high)/2), $temp = $list [$low];while ($low and Lt $high) {while ($low < $high && $list [$high] > $temp) {$high--;} $list [$low] = $list [$high];while ($low < $high && $list [$low] < $temp) {$low + +;} $list [$high] = $list [$low];} $list [$low] = $temp; return $low;} Heap sort function Heap (& $list) {buildheap ($list), for ($i = count ($list)-1; $i > 0; $i-) {swap ($list, $i, 0); Heapfy ($li St, 0, $i-1);}} Create Heap function Buildheap (& $list) {for ($i = (count ($list)-2)/2; $i >= 0; $i-) {heapfy ($list, $i, COUNT ($list)-1); }}//Maintenance Heap Function Heapfy (& $list, $low, $high) {$temp = $list [$low];for ($i = ($low * 2 + 1); $i <= $high; $i = ($i * 2 + 1) {if ($i < $high && $list [$i] < $list [$i + 1]) {$i + +;} if ($temp < $list [$i]) {swap ($list, $i, $low); $low = $i;} Else{break;}} $list [$low] = $temp;} Hill sort function Shell (& $list) {$a = 0; $code = count ($list)/3 + 1;while ($code >= 1) {for ($i = $code; $i < count ($lis T); $i + +) {$a ++;if ($list [$i] < $list [$i-$code]) {$temp = $list [$i]; $list [$i] = $list [$i-$code]; $j = $i -2* $code; $j >= 0 && $list [$j] > $temp; $j-= $code) {$list [$j + $code] = $list [$j]; $a + +;} $list [$j + $code] = $temp;}} $code = $code/3;} echo $a;} Direct Insert Sort function Insert (& $list) {$a = 0;for ($i = 1; $i < count ($list); $i + +) {$a ++;if ($list [$i] < $list [$i-1]) {$temp = $list [$i]; $list [$i] = $list [$i-1]; $j = $i -2;for (; $list [$j] > $temp; $j-) {$a + +; $list [$j + 1] = $list [$j ];} $list [$j + 1] = $temp;}} echo $a;} Simple Select Sort function Select (& $list) {$a = 0;for ($i = 0; $i < count ($list); $i + +) {$min = $i; $a ++;for ($j = $i + 1; $j &L T Count ($list); $j + +) {$a ++;if ($list [$j] < $list [$min]) {$min = $j;}} if ($min! = $i) Swap ($list, $i, $min);} echo $a;} Bubble Sort FunctIon Bubble (& $list) {$swap = true; $a = 0;for ($i = 0; $i < count ($list) && $swap; $i + +) {$swap = false; $a ++;f or ($j = count ($list)-2; $j >= $i; $j-) {$a ++;if ($list [$j] > $list [$j + 1]) {$swap = True;swap ($list, $j, $j + 1); }}}echo $a;} Move or Swap function swap (& $list, $i, $j) {$temp = $list [$i]; $list [$i] = $list [$j]; $list [$j] = $temp;}? > >>> For more information, check out the PHP array sorting method Daquan <<< |