Several algorithms that you don't need to know (PHP version)

Source: Internet
Author: User

<?PHP//1. Bubble sortfunctionBubble_sort ($arr) {$n=Count($arr);  for($i= 0;$i<$n-1;$i++) { for($j=$i+1;;$j<$n-$i;$j++) {if($arr[$j]<$arr[$i]) {$temp=$arr[$i]; $arr[$i] =$arr[$j]; $arr[$j] =$temp; } }}}//2. Merge sort//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 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 to the end must have a few values,//not the rest of the arra inside is the remaining ARRB inside and these several ordered values, certainly more than ARRC inside all the value is big so use$arrC[] =$arrA[' 0 '] <$arrB[' 0 ']?Array_shift($arrA) :Array_shift($arrB);}return Array_merge($arrC,$arrA,$arrB);}//Merge Sort Main programfunctionAl_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 fraction 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);//after splitting the left, start the recursive merge and go up .$right _arr= Al_merge_sort ($right _arr);//right split, start recursion, go up .$arr= Al_merge ($left _arr,$right _arr);//Merge two arrays, continue recursionreturn $arr;}$arr=Array(12, 5, 4, 7, 8, 3, 4, 2, 6, 4, 9);Print_r(Al_merge_sort ($arr)); //3, binary Search-recursive//Two-point lookup-recursionfunctionBin_search ($array,$low,$high,$k){    if($low<=$high){        $mid=intval(($low+$high)/2); }Else{        return false; }    if($array[$mid] ==$k){        return $mid; }ElseIf($k<$array[$mid]){        returnBin_search ($array,$low,$mid-1,$k); }Else{        returnBin_search ($array,$mid+1,$high,$k); }}$arr=Array(12, 5, 4, 7, 3, 8, 4, 2, 6, 4, 9);$index= Bin_search ($arr, 0,10,12);//Direct output is empty, puzzledEcho(intval($index));//4, two-part lookup-non-recursivefunctionBin_search ($arr,$low,$high,$value) {//$arr Array, $slow minimum index, $high maximum index $value lookup value     while($low<=$high) {$mid=intval(($low+$high)/2); if($value==$arr[$mid]) {return $mid; }ElseIf($value<$arr[$mid]) {$high=$mid-1; }Else{$low=$mid+1; }}return false; } //5. Quick SortfunctionQuick_sort ($arr) {    $n=Count($arr); if($n<=1)        return $arr; $key=$arr[0]; $left _arr=Array(); $right _arr=Array();  for($i= 1;$i<$n;$i++) {        if($arr[$i]<=$key)            $left _arr[]=$arr[$i]; Else            $right _arr[]=$arr[$i]; }    $left _arr=quick_sort ($left _arr); $right _arr=quick_sort ($right _arr); return Array_merge($left _arr,Array($key),$right _arr);}//6. Select SortfunctionSelect_sort ($arr) {    $n=Count($arr);  for($i= 0;$i<$n;$i++) {        $k=$i;  for($j=$i+1;$j<$n;$j++) {           if($arr[$j]<$arr[$k])               $k=$j; }        if($k!=$i) {            $temp=$arr[$i]; $arr[$i]=$arr[$k]; $arr[$k]=$temp; }    }    return $arr;}//7. Insert SortfunctionInsertsort ($arr) {    $n=Count($arr);  for($i= 1;$i<$n;$i++) {        $tmp=$arr[$i]; $j=$i-1;  while($arr[$j]>$tmp) {            $arr[$j+1]=$arr[$j]; $arr[$j]=$tmp; $j--; if($j<0)                 Break; }    }    return $arr;}

Several algorithms that you don't need to know (PHP version)

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.