Many people have said that the algorithm is the core of the program, the quality of the algorithm determines the process. As a primary phper, although little contact with algorithmic aspects. But for the basic sorting algorithm or should be mastered, it is the necessary tool for program development. The following through this article to introduce PHP to achieve four basic sorting algorithm of the running time comparison, together look at it.
No more nonsense to say, directly to everyone paste code.
The specific code looks like this:
/** * PHP four basic sorting algorithms run time comparison * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14///bubble Sort function Bubblesort ($ Array) {$temp = 0; for ($i = 0; $i < count ($array)-1; $i + +) {for ($j = 0; $j < count ($array)-1-$i; $j + +) {if ($array $j
] > $array [$j +1]) {//From small to large $temp = $array [$j]; $array [$j] = $array [$j +1]; $array [$j +1] = $temp;}} }//Select Sort function Selectsort ($array) {$temp = 0; for ($i = 0; $i < count ($array)-1; $i + +) {$minVal = $array [$i];
$i is the minimum value $minValIndex = $i; for ($j = $i +1; $j < count ($array); $j + +) {if ($minVal > $array [$j]) {//From small to large arrange $minVal = $array [$j];//Find minimum value $minValInde
x = $j;
}} $temp = $array [$i];
$array [$i] = $array [$minValIndex];
$array [$minValIndex] = $temp; }//Insert Sort function Insertsort ($array) {//From small to large array///first default $array[0], ordered table for ($i = 1; $i < count ($array); $i + +) {$inser Tval = $array [$i]; $insertVal is the number of $insertIndex to be inserted = $i-1; The subscript while ($insertIndex >= 0 && $insertVal < $array [$insertIndex]) of the number being compared in an ordered table {$array [$insertIndex + 1] = $array [$insertIndex]; Move the array backward $insertIndex-;
Move the subscript forward to compare with the previous one} if ($insertIndex + 1!== $i) {$array [$insertIndex + 1] = $insertVal; Quick Sort function QuickSort ($array) {if (!isset ($array [1])) return $array $mid = $array [0];//Get a keyword for segmentation, typically the first element $l
Eftarray = Array ();
$rightArray = Array (); foreach ($array as $v) {if ($v > $mid) $rightArray [] = $v;//Put the number larger than $mid into an array if ($v < $mid) $leftArray [] = $v;//$mi D A small number into another array} $leftArray = QuickSort ($leftArray); The smaller array is segmented again $leftArray [] = $mid; Add the split element to the back of the small array and don't forget it $rightArray = QuickSort ($rightArray); The larger array is again divided into return Array_merge ($leftArray, $rightArray); Combine two results} $a = Array_rand (range (1,3000), 1600); Generates a random array of 1600 elements shuffle ($a);
The order of the array is scrambled $t 1 = microtime (true); Bubblesort ($a);
Bubble sort $t 2 = Microtime (true); echo "bubble sort:". (($t 2-$t 1) *1000). ' Ms '. '
\ n ";
$t 3 = Microtime (true); Selectsort ($a);
Select the sort $t 4 = Microtime (true); echo "Select sort When:". (($t 4-$t 3) *1000). ' Ms '. '
\ n ";
$t 5 = Microtime (true); InsertSort ($a);
Insert Sort $t 6 = Microtime (true); echo "Insert sort when:". (($t 6-$t 5) *1000). ' Ms '. '
\ n ";
$t 7 = Microtime (true); QuickSort ($a);
Quick Sort $t 8 = Microtime (true); echo "Quick sort when:". (($t 8-$t 7) *1000). ' Ms ';
The above is a small set of PHP to introduce the implementation of four kinds of basic sorting algorithm running time comparison, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!