Common PHP interview algorithms and php interviews
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
1 <? Php 2 3 function mysort ($ arr) 4 {5 for ($ I = 0; $ I <count ($ arr); $ I ++) 6 {7 $ isSort = false; 8 for ($ j = 0; $ j <count ($ arr)-$ I-1; $ j ++) 9 {10 if ($ arr [$ j] <$ arr [$ j + 1]) 11 {12 $ isSort = true; 13 $ temp = $ arr [$ j]; 14 $ arr [$ j] = $ arr [$ j + 1]; 15 $ arr [$ j + 1] = $ temp; 16} 17} 18 if ($ isSort) 19 {20 break; 21} 22} 23 return $ arr; 24} 25 26 $ arr = array (3,1, 2); 27 var_dump (mysort ($ arr )); 28?>View Code
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
1 <? Php 2 // quick sorting 3 function quick_sort ($ arr) 4 {5 // first determine whether to continue with 6 $ length = count ($ arr ); 7 if ($ length <= 1) 8 {9 return $ arr; 10} 11 12 $ base_num = $ arr [0]; // select a ruler and select the first element 13 14 // initialize two arrays 15 $ left_array = array (); // less than the ruler's 16 $ right_array = array (); // 17 for ($ I = 1; $ I <$ length; $ I ++) 18 beyond the ruler, add 19 if ($ base_num> $ arr [$ I]) in two arrays based on the Size relationship. 20 {21 // put the left array 22 $ left_array [] = $ arr [$ I]; 23} 24 else 25 {26 // put 27 $ right_array [] = $ arr [$ I] on the right; 28} 29} 30 // sort the array on the left and right respectively in the same way 31 // recursively call this function, record the Result 32 $ left_array = quick_sort ($ left_array); 33 $ right_array = quick_sort ($ right_array); 34 // merge the 35 return array_merge ($ left_array, array ($ base_num), $ right_array); 36} 37 38 $ arr = array (3, 1, 2); 39 var_dump (quick_sort ($ arr); 40 41?>View Code
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
1 <? Php 2 // Binary Search 3 function bin_search ($ arr, $ low, $ high, $ k) 4 {5 if ($ low <= $ high) 6 {7 $ mid = intval ($ low + $ high)/2); 8 if ($ arr [$ mid] ==$ k) 9 {10 return $ mid; 11} 12 else if ($ k <$ arr [$ mid]) 13 {14 return bin_search ($ arr, $ low, $ mid-1, $ k ); 15} 16 else17 {18 return bin_search ($ arr, $ mid + 1, $ high, $ k); 19} 20} 21 return-1; 22} 23 24 $ arr = array (,); 25 26 print (bin_search ($ arr, 3); 27?>View Code
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
1 <? Php 2 // search for 3 function seq_search ($ arr, $ n, $ k) 4 {5 $ array [$ n] = $ k; 6 for ($ I = 0; $ I <$ n; $ I ++) 7 {8 if ($ arr [$ I] ==$ k) 9 {10 break; 11} 12} 13 14 if ($ I <$ n) 15 {16 return $ I; 17} 18 else19 {20 return-1; 21} 22} 23?>View Code
5. Write a function to traverse all files and subfolders in a file
1 <? Php 2 function my_scandir ($ dir) 3 {4 $ files = array (); 5 if ($ handle = opendir ($ dir )) 6 {7 while ($ file = readdir ($ handle ))! = False) 8 {9 if ($ file! = '..' & $ File! = '. ') 10 {11 if (is_dir ($ dir. "/". $ file) 12 {13 $ files [$ file] = my_scandir ($ dir. "/". $ file); 14} 15 else16 {17 $ files [] = $ file; 18} 19} 20} 21 22 closedir ($ handle); 23 return $ files; 24} 25} 26 27 var_dump (my_scandir ('.. /'); 28?>View Code
6. Write a function to retrieve the file extension from a standard url as efficiently as possible
1 <? Php 2 function getExt ($ url) 3 {4 $ arr = parse_url ($ url); // parse_url parses a URL and returns an associated array, various components contained in the URL 5 // 'scheme '=> string 'HTTP' (length = 4) 6 // 'host' => string 'www .sina.com.cn '(length = 15) 7 // 'path' => string'/abc/de/fg. php '(length = 14) 8 // 'query' => string 'id = 1' (length = 4) 9 $ file = basename ($ arr ['path']); // The part of the file name in the returned path of the basename function 10 $ ext = explode ('. ', $ file); 11 return $ ext [count ($ ext) -1]; 12} 13 14 print (getExt ('HTTP: // www.sina.com.cn/abc/de/fg.html.php? Id = 1 '); 15 16?>View Code
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.