PHP four basic sorting algorithm run time comparison!

Source: Internet
Author: User

/*** PHP four basic sorting algorithm run time comparison*@authorsJesse (jesse152@163.com)*@date2016-08-11 07:12:14*/Bubble Sort Methodfunction 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]){Arrange from small to large$temp =$array[$j];$array[$j] =$array[$j+1];$array[$j+1] =$temp; } } }}Select Sort methodfunction Selectsort($array){$temp =0;For($i =0;$i <Count($array) -1;$i + +){$minVal =$array[$i];Assuming $i is the minimum value$minValIndex =$i;For($j =$i+1;$j <Count($array);$j + +){If($minVal >$array[$j]){Arrange from small to large$minVal =$array[$j];Find Minimum value$minValIndex =$j; } }$temp =$array[$i];$array[$i] =$array[$minValIndex];$array[$minValIndex] =$temp; }}Insert Sort methodfunction Insertsort($array){Arrange from small to largeFirst default $array[0], already ordered, is an ordered tableFor($i =1;$i <Count($array);$i + +){$insertVal =$array[$i];$insertVal is the number that is ready to be inserted$insertIndex =$i-1;Subscript for the number of comparisons to be prepared in an ordered tableWhile($insertIndex >=0 &&$insertVal <$array[$insertIndex]){$array[$insertIndex +1] =$array[$insertIndex];Move the array backwards$insertIndex--;Move the subscript forward, ready to compare with the previous one}If($insertIndex +1!==$i){$array[$insertIndex +1] =$insertVal; } }}Quick Sort Methodfunction QuickSort($array){If(!Isset($array[1]))Return$array;$mid =$array[0];Gets a keyword for segmentation, typically the first element$leftArray =Array();$rightArray =Array();Foreach($arrayAs$v){If($v >$mid)$rightArray[] =$v;Put a number larger than $mid into an array.If($v <$mid)$leftArray[] =$v;Put a number smaller than $mid in another array}$leftArray = QuickSort($leftArray);Split the smaller array once again$leftArray[] =$mid;Add the split element to the small array, and don't forget it.$rightArray = QuickSort($rightArray);Split the larger array once againReturnArray_merge($leftArray,$rightArray);Combination of two results}$a =Array_rand(Range(1,3000),1600);Generates a random array of 1600 elementsShuffle($a);Order of scrambled arrays$t 1 =Microtime(True); Bubblesort($a);Bubble sort$t 2 =Microtime(True);Echo"Bubble sort Time:".(($t 2-$t 1)*1000).' Ms '."\ n";$t 3 =Microtime(True); Selectsort($a);Select sort$t 4 =Microtime(True);Echo"Select Sort time:".(($t 4-$t 3)*1000).' Ms '."\ n";$t 5 =Microtime(True); Insertsort($a);Insert Sort$t 6 =Microtime(True);Echo"Insert sort time:".(($t 6-$t 5)*1000). ' Ms '.  "\n " $t 7 = microtime (true    (  $a   //Quick sort   $t 8 =  microtime   ( true   echo   "quick sort time:".   ((  $t 8-  $t 7  ) *  Span class= "DV" >1000  ).   ' MS '              

PHP four basic sorting algorithm run time comparison!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.