PHP Common sort and find algorithm, PHP sorting algorithm
This paper summarizes the common PHP sorting algorithm and lookup, in the design of the algorithm has a good reference value. We are now sharing it for your reference. Specific as follows:
<?php/** * PHP most commonly used four sorting methods and two search methods * The following sorting methods all pass the test * auther:soulence * DATE:2015/06/20 *///php Bubble Sort method function Bubbleso RT (& $arr) {//This is an intermediate variable $temp = 0;//We want to sort the array, from small to large//outer loop $flag =false;//This optimization will be very efficient, generally enough for ($i =0; $i
$arr [$j +1]) {$temp = $arr [$j]; $arr [$j]= $arr [$j +1]; $arr [$j +1]= $temp; $flag =true; }} if (! $flag) {//is already ordered break; } $flag =false; }}//php Select Sort method efficiency is higher than bubbling function Selectsort (& $arr) {$temp = 0; for ($i =0; $i
$arr [$j]) {$minVal = $arr [$j]; $minIndex = $j; }}//Last Exchange $temp = $arr [$i]; $arr [$i]= $arr [$minIndex]; $arr [$minIndex]= $temp; }}//Insert Sort method (small to large sort) the efficiency is higher than the sorting method. Function Insertsort (& $arr) {////default subscript 0 is already ordered for ($i =1; $i
= 0 && $insertVal < $arr [$inserIndex]) {//shift the number back $arr [$inserIndex +1] = $arr [$inserIndex]; $inserIndex--; }//Insert (at this point to find the appropriate location for $inserindex) $arr [$inserIndex +1] = $insertVal; }}//Quick Sort method The first is not the function quickSort ($left, $right,& $arr) {$l = $left; $r = $right; $pivot = $arr [($left + $right)/2]; while ($l < $r) {while ($arr [$l]< $pivot) {$l + +; } while ($arr [$r]> $pivot) {$r--; } if ($l >= $r) {break; } $temp = $arr [$l]; $arr [$l]= $arr [$r]; $arr [$r]= $temp; if ($arr [$l]== $pivot) {--$r; if ($arr [$r]== $pivot) {+ + $l; }} if ($l = = $r) {$l + +; $r--; } if ($left < $r) QuickSort ($left, $r, $arr); if ($right > $l) quickSort ($l, $right, $arr);} /** * Quick Sort method Implementation of the second method of implementation of the * PHP Quick Sort method * $order ASC Small to large desc large to small default is ASC * $order value can only be ASC desc If you write a value is also sorted by ASC */function QuickSort2 ($arr, $order = ' asc ') {if (count ($arr) <= 1) return $arr; $arr _left = $arr _right = Array ();$val = $arr [0];unset ($arr [0]); foreach ($arr as $v) {if (Strtolower ($order) = = ' desc ') {if ($v < $val) $arr _right[] = $v; else $arr _left[] = $v; }else{if ($v > $val) $arr _right[] = $v; else $arr _left[] = $v; }} $arr _left = QuickSort ($arr _left, $order); $arr _right = QuickSort ($arr _right, $order); Return Array_merge ($arr _left,array ($val), $arr _right);} Here is the lookup $arr=array (46,90,900,0,-1);//This is a sequential query for function search (& $arr, $findVal) {$flag =false; for ($i =0; $i
< $leftindex条件成立,说明没有这个数,则退出="" if($rightindex="" $leftindex){="" "找不到该数";="" return;="" 首先找到中间这个数="" round是出于如果出现小数,四舍五入="" $middleindex="round(($rightIndex+$leftIndex)/2);" 如果大于则向后面找="">$arr [$middleIndex]) {BinarySearch ($arr, $findVal, $middleIndex +1, $rightIndex); If it is less than the middle number, look to the front}else if ($findVal < $arr [$middleIndex]) {BinarySearch ($arr, $findVal, $leftIndex, $middleIndex-1); }else{echo "Find this number. Subscript is $middleindex "; }}?>
It is hoped that the sorting algorithm and the example of finding algorithm in this paper will help the PHP program design.
http://www.bkjia.com/PHPjc/1044259.html www.bkjia.com true http://www.bkjia.com/PHPjc/1044259.html techarticle php Common sort and find algorithm, PHP sorting algorithm This article summarizes the common PHP sorting algorithm and search, in the design of the algorithm has a good reference value. We are now sharing ...