This is a php interview question for a real IT company recruiting php programmers. all the questions are about algorithms. To be honest, these are simple basic algorithms! The following php interview questions are all provided with answers. please consider them yourself! (1) use PHP to describe bubble sorting and quick sorting algorithms, object
This is a php interview question for a real IT company recruiting php programmers. all the questions are about algorithms. To be honest, these are simple basic algorithms!
The followingPhp interview questionsThe answer is provided. please consider it yourself!
(1) use PHP to describe the bubble sort and quick sort algorithms. the object can be an array.
// 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 ;}
Function quick_sort ($ array) {if (count ($ array) <= 1) return $ array; $ key = $ array [0]; $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I
(2) using PHP to describe sequential search and binary search algorithms, efficiency must be taken into account for sequential search. the object can be an ordered array.
// Use binary search for 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 ;}
// Search for an element in the array in sequence: 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 ;}}
3. write a two-dimensional array sorting algorithm function that can call the php built-in function,Versatility
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;}
The above php interview questions are about algorithms and collected on the internet. if you have a better solution, please leave a message to me!