A summary of common sorting algorithms in PHP ,. Summary of common sorting algorithms in PHP versions. 1. Bubble sorting functionbubble_sort ($ arr) {$ ncount ($ arr); for ($ i0; $ I $ n-1; $ I ++) {for ($ j $ I + 1; $ j $ n-$ I; $ j ++) {if ($ arr [$ j] $ arr [$ I]) {$ t summary of common sorting algorithms in PHP,
// 1. Bubble sorting
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 and sort
// The merge function merges and sorts the specified two ordered arrays (arr1arr2,). // we can find the third array, then, retrieve the data starting from the two arrays in sequence, and then delete the data function al_merge ($ arrA, $ arrB) that has just obtained) {$ arrC = array (); while (count ($ arrA) & count ($ arrB) {// you can determine which value is small, the small value is given to arrC, but at the end of the day, there must be a few remaining values. // not the remaining values in arrA are those in the remaining arrB and these ordered values, it must be larger than all values in arrC. so use $ arrC [] = $ arrA ['0'] <$ arrB ['0']? Array_shift ($ arrA): array_shift ($ arrB);} return array_merge ($ arrC, $ arrA, $ arrB);} // combiner sort main program function al_merge_sort ($ arr) {$ len = count ($ arr); if ($ len <= 1) {return $ arr; // recursive end condition, when this step is reached, only one element is left in the array, that is, the array is separated} $ mid = intval ($ len/2); // Get $ left_arr = array_slice ($ arr, 0, $ mid); // split array 0-mid this part to left left_arr $ right_arr = array_slice ($ arr, $ mid ); // split the array mid-end to the right side of the right_arr $ left_arr = random ($ left_arr); // after the left side is split, start recursive merge and go up to $ right_arr = al_merge_sort ); // after splitting on the right, start recursively going up $ arr = al_merge ($ left_arr, $ right_arr); // merge two arrays and continue recursion return $ arr ;} $ arr = array (12, 5, 4, 7, 8, 3, 4, 2, 6, 4, 9); print_r (al_merge_sort ($ arr ));
// 3. binary search-recursion
// Binary search-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 (12, 5, 4, 7, 3, 8, 4, 2, 6, 4, 9); $ index = bin_search ($ arr, 12); // The direct output is empty, echo (intval ($ index ));
// 4. binary search-non-recursive
Function bin_search ($ arr, $ low, $ high, $ value) {// $ arr array; $ slow minimum index; $ high maximum index $ value the searched 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. fast sorting
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 sorting
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 sorting
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;}
Articles you may be interested in:
- PHP sorting algorithm implementation code
- PHP bubble sorting algorithm implementation code
- Php sorting algorithm (bubble sorting, fast sorting)
- PHP bubble sorting binary search sequence query two-dimensional array sorting algorithm function details
- PHP quick sorting algorithm details
- PHP simple sorting algorithm instance selection
Sort, // 1. Bubble sorting 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]) {$ t...