Objective
The following is the implementation of the classic algorithm through PHP, and calculate the time-consuming, can be time-consuming comparison of the complexity of these algorithms.
CODE
$arr = [];for ($i = 0; $i <; $i + +) {$arr [] = rand (1, 10000);} 1 Insert Sort function Insertionsort ($arr) {for ($i = 1; $i < count ($arr); $i + +) {$tmp = $arr [$i];//Set up a lookout $key = $i-1; Set the location to start looking while ($key >= 0 && $tmp < $arr [$key]) {//The lookout value is smaller than the value found and not to the first $arr of the query [$k EY + 1] = $arr [$key]; The value of the array is moved back $key-; To find the position to move back} if (($key + 1)! = $i)//Place the watch $arr [$key + 1] = $tmp; } return $arr;} $insertion _start_time = Microtime (True), $insertion _sort = Insertionsort ($arr), $insertion _end_time = Microtime (true); $insertion _need_time = $insertion _end_time-$insertion _start_time;print_r ("Insertion sort time:". $insertion _need_time. "
");//2 bubble sort function Bubblesort ($arr) {for ($i = 0; $i < count ($arr); $i + +) {for ($j = 0; $j < $i + 1; $j + +) {if ($arr [$j] < $arr [$j-1]) {$temp = $a rr[$j-1]; $arr [$j-1] = $arr [$j]; $arr [$j] = $temp; }}} return $arr;} $bubble _start_time = Microtime (True), $bubble _sort = Bubblesort ($arr), $bubble _end_time = Microtime (true); $bubble _need _time = $bubble _end_time-$bubble _start_time;print_r ("Bubble sort Time:". $bubble _need_time. "
");//3 Select Sort function Selectionsort ($arr) {$count = count ($arr); for ($i = 0; $i < $count-1; $i + +) {//find minimum value $min = $i; for ($j = $i + 1; $j < $count; $j + +) {//From small to large arrange if ($arr [$min] > $arr [$j]) {// Indicates that the current smallest is larger than the current element $min = $j; Assign a new minimum}/*swap$array[$i]and$array[$min] Place the smallest element of the current loop in the $i position */if ($min! = $i) { $temp = $arr [$min]; $arr [$min] = $arr [$i]; $arr [$i] = $temp; }} return $arr;} $selection _start_time = Microtime (True), $selection _sort = Selectionsort ($arr), $selection _end_time = Microtime (true); $selection _need_time = $selection _end_time-$selection _start_time;print_r ("Select sort time consuming:". $selection _need_time. "
");//4 the sorted//merge function merges and sorts the specified two ordered arrays (ARR1ARR2,)//We can find the third array, then take the data from the beginning of the two arrays, whichever data is smaller, and then delete the data function that was just taken/// Al_merge ($arrA, $arrB) {$arrC = array (); while (count ($arrA) && count ($arrB)) {//Here constantly judging which value is small, give the small value to ARRC, but there must be a few values left in the end,//not the rest of ArrA inside is the rest of arr b inside and these ordered values are certainly larger than all the values inside the ArrC so use $arrC [] = $arrA [' 0 '] < $arrB [' 0 ']? Array_shift ($arrA): Array_shift ($arrB); } return Array_merge ($arrC, $arrA, $arrB);} Merge sort main program function Al_merge_sort ($arr) {$len = count ($arr); if ($len <= 1) return $arr;//recursive end condition, when this step is reached, the array is left with only one element, that is, the array is separated $mid = Intval ($len/2);//Take the middle of the array $left _arr = Array_slice ($arr, 0, $mid);//split the fractional group 0-mid this part to the left Left_arr $right _arr = Array_slice ($arr, $mid);//split the fractional group mid-at the end of this section to the right right_a RR $left _arr = Al_merge_sort ($left _arr);//The left side of the split after the beginning of the recursive merge to go up $right _arr = Al_merge_sort ($right _arr);//The right split is complete start recursion $arr = Al_merge ($left _arr, $right _arr);//merge two arrays, continue recursive return $arr;} $merge _start_time = Microtime (true); $merge _sort =Al_merge_sort ($arr); $merge _end_time = Microtime (true); $merge _need_time = $merge _end_time-$merge _start_time;print_r ("and sequencing time-consuming:".) $merge _need_time. "
");//5 Quick Sort function QuickSort (& $arr) {if (count ($arr) >1) {$k = $arr [0]; $x =array (); $y =array (); $_size=count ($arr); for ($i =1; $i <$_size; $i + +) {if ($arr [$i]<= $k) {$x []= $arr [$i]; }elseif ($arr [$i]> $k) {$y []= $arr [$i]; }} $x =quicksort ($x); $y =quicksort ($y); Return Array_merge ($x, Array ($k), $y); }else{Return$arr; }} $quick _start_time = Microtime (True), $quick _sort = Al_merge_sort ($arr), $quick _end_time = Microtime (True); $quick _ Need_time = $quick _end_time-$quick _start_time;print_r ("Quick sort time:". $quick _need_time. "
");
Time-consuming comparisons
Insert Sort time: 1.2335460186005
Bubble sort Time: 2.6180219650269
Select sort Time: 1.4159741401672
and sequencing time: 0.17212891578674
Quick Sort Time: 0.16736888885498