Common sorting algorithms in PHP

Source: Internet
Author: User
This article mainly introduces PHP in the common sorting algorithm, interested in the reference of friends, I hope to be helpful to everyone.

1. Bubble sort

function Bubble_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 a third array, Then take the data from the beginning of the two arrays, whichever data is small, and then delete the data function Al_merge ($arrA, $arrB) {$arrC = Array (), which has just been taken, and then (count ($arrA) & & Count ($arrB)) {//Here constantly judge which value is small, will be small value to ARRC, but to the end must be left a few values,//is not left arra inside is the left ARRB inside and these several ordered values, Definitely bigger than all the values in 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 has only one element left. That is, separate the array} $mid = Intval ($len/2); Take array middle $left_arr = Array_slice ($arr, 0, $mid); Split fraction Group 0-mid This section 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_arr$left_arr = Al_merge_sort ($left _arr); After splitting the left, start the recursive merge to go up $right_arr = Al_merge_sort ($right _arr); The right split is finished and the recursion goes up $arr = Al_merge ($left _arr, $right _arr); Merge two arrays, continue recursive return $arr;} $arr = Array (5, 4, 7, 8, 3, 4, 2, 6, 4, 9);p Rint_r (Al_merge_sort ($arr)); 

3, binary search-recursion

Binary Lookup-recursive function Bin_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]) {    return Bin_search ($array, $low, $mid-1, $k);  } else{    return Bin_search ($array, $mid +1, $high, $k);  }} $arr = Array (5, 4, 7, 3, 8, 4, 2, 6, 4, 9); $index = Bin_search ($arr, 0,10,12); The direct output is empty and does not understand Echo (Intval ($index));

4, two-part lookup-non-recursive

function Bin_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 Sort

function Quick_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 Sort

function Select_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 Sort

function Insertsort ($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;}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.