This article provides a detailed analysis of PHP bubble sorting binary search sequence query two-dimensional array sorting algorithm functions. it is important for a friend to refer to the data structure, algorithm + data structure + document = program
Describes the bubble sorting algorithm using PHP. the object can be an array.
// Bubble sorting (array sorting) function bubble_sort ($ array) {$ count = count ($ array); if ($ count <= 0) return false; for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = $ count-1; $ j> $ I; $ j -) {if ($ array [$ j] <$ array [$ j-1]) {$ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j-1]; $ array [$ j-1] = $ tmp ;}} return $ array ;}
Using PHP to describe sequential search and binary search (also called semi-query) algorithms, the efficiency of sequential search must be considered. the object can be an ordered array.
// Binary search (find an element in the array) function bin_sch ($ array, $ low, $ high, $ k) {if ($ low <= $ high) {$ mid = intval ($ low + $ high)/2); if ($ array [$ mid] ==$ k) {return $ mid ;} elseif ($ k <$ array [$ mid]) {return bin_sch ($ array, $ low, $ mid-1, $ k);} else {return bin_sch ($ array, $ mid + 1, $ high, $ k) ;}} return-1 ;}// sequential search (find an element in the array) function seq_sch ($ array, $ n, $ k) {$ array [$ n] = $ k; for ($ I = 0; $ I <$ n; $ I ++) {if ($ array [$ I] ==$ k) {break ;}} if ($ I <$ n) {return $ I ;} else {return-1 ;}}
Write a two-dimensional array sorting algorithm function that is universal and can call php built-in functions
// Two-dimensional array sorting, $ arr is data, $ keys is the key value of sorting, $ order is the sorting rule, 1 is the ascending order, and 0 is the descending order function array_sort ($ arr, $ keys, $ order = 0) {if (! Is_array ($ arr) {return false;} $ keysvalue = array (); foreach ($ arr as $ key => $ val) {$ keysvalue [$ key] = $ val [$ keys];} if ($ order = 0) {asort ($ keysvalue );} else {arsort ($ keysvalue);} reset ($ keysvalue); foreach ($ keysvalue as $ key => $ vals) {$ keysort [$ key] = $ key ;} $ new_array = array (); foreach ($ keysort as $ key => $ val) {$ new_array [$ key] = $ arr [$ val];} return $ new_array ;}
For more details about PHP bubble sorting binary search sequence query two-dimensional array sorting algorithm functions, please follow the PHP Chinese network!