Common PHP interview algorithms (recommended) and php interview Algorithms

Source: Internet
Author: User

Common PHP interview algorithms (recommended) and php interview Algorithms

I. Bubble Sorting

Basic Idea:

The array to be sorted is scanned multiple times from the back to the back (backward). When the order of two adjacent values is found to be different from the sorting rule, the two values are exchanged. In this way, a relatively small (large) value will gradually move from the back to the front.

// Bubble sort

<?php  function mysort($arr)  {    for($i = 0; $i < count($arr); $i++)    {      $isSort = false;      for ($j=0; $j< count($arr) - $i - 1; $j++)       {        if($arr[$j] < $arr[$j+1])        {          $isSort = true;          $temp = $arr[$j];          $arr[$j] = $arr[$j+1];          $arr[$j+1] = $temp ;        }      }      if($isSort)      {        break;      }    }    return $arr;  }  $arr = array(3,1,2);  var_dump(mysort($arr));?>

Ii. Quick sorting

Basic Idea:

Pick out an element (mostly the first one) in the array as the ruler. Scan the array to rank the elements smaller than the ruler before the ruler, and rank all the elements larger than the ruler after the ruler, subsequences are divided into smaller sequences by recursion until all sequences are in the same order.

// Quick sorting

<? Php // quick sorting function quick_sort ($ arr) {// first judge whether to continue $ length = count ($ arr); if ($ length <= 1) {return $ arr;} $ base_num = $ arr [0]; // select the first element for a ruler. // initialize two arrays $ left_array = array (); // $ right_array = array (); // for ($ I = 1; $ I <$ length; $ I ++) smaller than the ruler) {// traverse all elements except the ruler and put them in two arrays according to the size relationship if ($ base_num> $ arr [$ I]) {// put $ left_array [] = $ arr [$ I];} else {// put $ right_array [] = $ arr [$ I] on the right;} // perform the following operations on the left and right respectively. // Recursively call this function, and record the result $ left_array = quick_sort ($ left_array); $ right_array = quick_sort ($ right_array ); // merge return array_merge ($ left_array, array ($ base_num), $ right_array);} $ arr = array (3, 1, 2 ); var_dump (quick_sort ($ arr);?>

Iii. Binary Search

Basic Idea:

Assume that the data is sorted in ascending order. For the given value x, the comparison starts from the intermediate position of the sequence. If the current position value is equal to x, the query is successful. If x is less than the current position value, search in the first half of the series; if x is greater than the current position value, continue searching in the second half of the series until it is found. (When the data volume is large)

// Binary Search

<? Php // binary search function bin_search ($ arr, $ low, $ high, $ k) {if ($ low <= $ high) {$ mid = intval ($ low + $ high)/2); if ($ arr [$ mid] ==$ k) {return $ mid ;} else if ($ k <$ arr [$ mid]) {return bin_search ($ arr, $ low, $ mid-1, $ k );} else {return bin_search ($ arr, $ mid + 1, $ high, $ k) ;}} return-1 ;}$ arr = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); print (bin_search ($ arr, 3);?>

Iv. Sequential search

Basic Idea:

The search starts from the first element of the array one by one. If the element is consistent with the target, the search is successful. If the last element still has no target element, the search fails.

// Sequential search

<? Php // search function seq_search ($ arr, $ n, $ k) {$ array [$ n] = $ k; for ($ I = 0; $ I <$ n; $ I ++) {if ($ arr [$ I] ==$ k) {break ;}} if ($ I <$ n) {return $ I;} else {return-1 ;}}?>

5. Write a function to traverse all files and subfolders in a file

<?php    function my_scandir($dir)  {    $files = array();    if($handle = opendir($dir))    {      while (($file = readdir($handle))!== false)       {        if($file != '..' && $file != '.')        {          if(is_dir($dir."/".$file))          {            $files[$file]=my_scandir($dir."/".$file);          }          else          {            $files[] = $file;          }        }      }      closedir($handle);      return $files;    }  }  var_dump(my_scandir('../'));?>

6. Write a function to retrieve the file extension from a standard url as efficiently as possible

<? Php function getExt ($ url) {$ arr = parse_url ($ url); // parse_url parses a URL and returns an associated array, various components contained in the URL // 'scheme '=> string 'http' (length = 4) // 'host' => string 'www .sina.com.cn '(length = 15) // 'path' => string'/abc/de/fg. php '(length = 14) // 'query' => string 'id = 1' (length = 4) $ file = basename ($ arr ['path']); // The part of the file name in the returned path of the basename function $ ext = explode ('. ', $ file); return $ ext [count ($ ext)-1];} print (getExt ( 'Http: // www.sina.com.cn/abc/de/fg.html.php? Id = 1 ');?>

VII. Methods for intercepting Chinese strings without garbled characters

Mb_substr can be used, but make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the number of undefined functions may occur.

The above common PHP interview algorithm (recommended) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the customer's house.

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.