PHP interview question algorithm analysis, php question Analysis _ PHP Tutorial

Source: Internet
Author: User
Algorithm Analysis of PHP interview questions and php question analysis. PHP interview question algorithm analysis, php question analysis what algorithms are frequently asked during the interview. here we integrate some common algorithms and their implementation principles. the following example shows the algorithm analysis of PHP interview questions.

What algorithms are frequently asked during interviews. some common algorithms and their implementation principles are integrated here. the following examples are available after testing. if you have any questions, please let us know !!

I am a little white. if you have a better implementation method, please feel free to give me some advice. thank you !!!!

Bubble sorting, quick sorting, select sorting, binary search, and quick search

/* Bubble sorting * The number of adjacent two is relatively small, the former is small, and the latter is large. * the array has several elements, we need to compare several rounds of $ I *. the number of times each round needs to be compared is, number of array elements-number of comparisons $ j * @ param array $ array to be operated * @ return array $ array returned array */function bubbleSort ($ array) {$ cnt = count ($ array); for ($ I = 0; $ I <$ cnt; $ I ++) {for ($ j = 0; $ j <($ cnt-$ i-1); $ j ++) {if ($ array [$ j]> $ array [$ j + 1]) {$ temp = $ array [$ j]; $ array [$ j] = $ array [$ j + 1]; $ array [$ j + 1] = $ temp; }}return $ array ;}

/* ** Quick sort * recursion * get the first number of arrays, and compare the following number in a loop. * put the number smaller than the number in an array, put it in an array larger than it * recursively call two arrays until the final array element is less than or equal to 1, there is no comparable element * through the array_merge function, merge the compared arrays in order of size and return them one by one layer, the final implementation of sorting from small to large * @ param array $ array to be operated * @ return array $ array returned array */function quickSort ($ array) {if (count ($ array) <= 1) return $ array; $ key = $ array [0]; $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I
 
  

/*** Select sort * Layer 2 loop * obtain array values one by one at the first layer $ array [$ I] * The second traversal of the entire array is compared with $ array [$ I] ($ j = $ I + 1 has been compared, no comparison, reduce the number of comparisons) * if it is smaller than $ array [$ I, switch the location * so that the minimum value in the array * can be obtained after one round. then, the entire outer loop is pushed internally and the array is sorted from small to large * @ param array $ array to be compared *@ return array $ array after ascending order */function selectSort ($ array) {$ cnt = count ($ array); for ($ I = 0; $ I <$ cnt; $ I ++) {for ($ j = ($ I + 1); $ j <$ cnt; $ j ++) {if ($ array [$ I]> $ array [$ j]) {$ tmp = $ array [$ I]; $ array [$ I] = $ array [$ j]; $ array [$ j] = $ tmp ;}} return $ array ;}

/*** Returns the position of a value in the array by using the * @ param array $ array operation array * @ param void $ val the value to be searched * @ return int $ mid index in the array, if no result exists, return-1 */function binarySearch ($ array, $ val) {$ cnt = count ($ array); $ low = 0; $ high = $ cnt-1; while ($ low <= $ high) {$ mid = intval ($ low + $ high)/2); if ($ array [$ mid] = $ val) {return $ mid;} if ($ array [$ mid] <$ val) {$ low = $ mid + 1;} if ($ array [$ mid]> $ val) {$ high = $ mid-1 ;}} return-1 ;}

/*** Sequential search (simplest and inefficient) * search for the expected value through the Cyclic array * @ param array $ array the array to be operated * @ param void $ val the value to be searched * @ return int if it exists, returns the index of the value in the array. otherwise, returns-1 */function seqSch ($ array, $ val) {for ($ I = 0; $ I
   
    

What algorithms are frequently asked during the workshop interview? some common algorithms and their implementation principles are integrated here. the following examples are available after testing...

Related Article

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.