Php interview question algorithm

Source: Internet
Author: User
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!

 

 

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.