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.
- Insert Sort
- Bubble sort
- Select sort
- and sorted
- Quick Sort
CODE
$arr= []; for($i=0;$i< the;$i++) {$arr[] = rand (1,10000);}//1 Insert Sort functioninsertionsort($arr){ for($i=1;$i< count ($arr);$i++) {$tmp=$arr[$i];//Set up a lookout$key=$i-1;//Set where to start looking while($key>=0&&$tmp<$arr[$key]) {//The value of the lookout is smaller than the value found and not to the first of this query$arr[$key+1] =$arr[$key];the value of the//array is moved back$key--;//The position to find is moved back}if(($key+1) !=$i)//Placing a lookout$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;p Rint_r ("Insert sort time:".$insertion _need_time."
");//2 Bubble Sort functionbubblesort($arr){ for($i=0;$i< count ($arr);$i++) { for($j=0;$j<$i+1;$j++) {if($arr[$j] <$arr[$j-1]) {$temp=$arr[$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;p Rint_r ("bubble sort time:".$bubble _need_time."
");//3 Select Sort functionselectionsort($arr){$count= Count ($arr); for($i=0;$i<$count-1;$i++) {//Find the smallest value$min=$i; for($j=$i+1;$j<$count;$j++) {//From small to large arrangementif($arr[$min] >$arr[$j]) {//indicates that the current smallest is also larger than the current element$min=$j;//Assign a new minimum} }/*swap$array[$i]and$array[$min] The smallest element in the current loop is placed 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;p Rint_r ("Select sort time:".$selection _need_time."
");//4 and Sortedthe//merge function merges and sorts the specified two ordered arrays (ARR1ARR2,)///We can find the third array, then fetch the data from the beginning of the two array which data is small, and then delete the data that has just been taken from/// functional_merge($arrA, $arrB){$arrC=Array(); while(Count ($arrA) && count ($arrB)) {//Here constantly judge which value is small, will be small value to ARRC, but in the end must be left a few values,//Not the rest of the arra inside is the rest of the ARRB inside and these several ordered values, certainly more than ARRC inside all the values are large, so use$arrC[] =$arrA[' 0 '] <$arrB[' 0 '] ? Array_shift ($arrA): Array_shift ($arrB); }returnArray_merge ($arrC,$arrA,$arrB);}//merge sort main program functional_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 score Group 0-mid this part to the left Left_arr$right _arr= Array_slice ($arr,$mid);//Split fraction group mid-end this part to the right Right_arr$left _arr= Al_merge_sort ($left _arr);//Left split up and start recursive merge go up$right _arr= Al_merge_sort ($right _arr);//Right split finish start recursion go up$arr= Al_merge ($left _arr,$right _arr);//Merge two arrays, continue recursionreturn$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;p Rint_r ("and sequencing time-consuming:".$merge _need_time."
");//5 Quick Sort functionquickSort(&$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);returnArray_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;p Rint_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
Resources
- Introduction to Algorithms
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above introduces the implementation of the classic PHP algorithm, including PHP, classic aspects of the content, I hope that the PHP tutorial interested in a friend helpful.