Common sorting and search algorithms in PHP, and php sorting algorithms. PHP's common sorting and searching algorithms. This article summarizes common php sorting algorithms and searching algorithms, which have good reference value for algorithm design. We will share with you the sorting and searching algorithms commonly used by PHP, and the php sorting algorithm.
This article summarizes common php sorting algorithms and searches, which provide a good reference for algorithm design. I will share it with you for your reference. The details are as follows:
<? Php/*** the four most common sorting methods and two searching methods in PHP * The following sorting methods all pass the test * auther: soulence * date: * // PHP bubble sort function bubbleSort (& $ arr) {// This is an intermediate variable $ temp = 0; // we want to put the array, sort from small to large // The outer loop $ flag = false; // After optimization, the efficiency will be very high, generally enough for ($ I = 0; $ I
$ Arr [$ j + 1]) {$ temp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j + 1]; $ arr [$ j + 1] = $ temp; $ flag = true ;}} if (! $ Flag) {// it is already sorted break;} $ flag = false;} // the efficiency of selecting a sorting method in PHP is higher than that in bubble mode. function selectSort (& $ arr) {$ temp = 0; for ($ I = 0; $ I
$ Arr [$ j]) {$ minVal = $ arr [$ j]; $ minIndex = $ j ;}// finally swap $ temp = $ arr [$ I]; $ arr [$ I] = $ arr [$ minIndex]; $ arr [$ minIndex] = $ temp ;}// insert sort (ascending order) efficiency is more efficient than selecting a sort method. function insertSort (& $ arr) {// The number of first subscript 0 is already ordered for ($ I = 1; $ I
= 0 & $ insertVal <$ arr [$ inserIndex]) {// move the number back at the same time $ arr [$ inserIndex + 1] = $ arr [$ inserIndex]; $ inserIndex --;} // Insert (locate $ inserIndex) $ arr [$ inserIndex + 1] = $ insertVal ;}} // The first method of quick sorting is not the function quickSort ($ left, $ right, & $ arr) {$ l = $ left; $ r = $ right; $ align = $ arr [($ left + $ right)/2]; while ($ l <$ r) {while ($ arr [$ l] <$ align) {$ l ++;} while ($ arr [$ r]> $ scheme) {$ r --;} if ($ l >=$ r) {break ;} $ temp = $ arr [$ l]; $ arr [$ l] = $ arr [$ r]; $ arr [$ r] = $ temp; if ($ arr [$ l] ==$ scheme) {-- $ r ;}if ($ arr [$ r] ==$ scheme) {++ $ l ;}} if ($ l ==$ r) {$ l ++; $ r -- ;}if ($ left <$ r) quickSort ($ left, $ r, $ arr ); if ($ right> $ l) quickSort ($ l, $ right, $ arr );} /*** quick sorting method * PHP quick sorting method * implemented by the second implementation method $ order asc is as small as desc is as large as desc. by default, the value of asc * $ order can only be asc desc if a random value is written, it is also sorted by asc */function quickSort2 ($ arr, $ order = 'asc ') {if (count ($ arr) <= 1) return $ arr; $ arr_left = $ arr_right = array (); $ val = $ arr [0]; unset ($ arr [0]); foreach ($ arr as $ v) {if (strtolower ($ order) = 'desc ') {if ($ v <$ val) $ arr_right [] = $ v; else $ arr_left [] = $ v;} else {if ($ v> $ val) $ arr_right [] = $ v; else $ arr_left [] = $ v ;}}$ arr_left = quickSort ($ arr_left, $ order); $ arr_right = quickSort ($ arr_right, $ order); return array_merge ($ arr_left, array ($ val), $ arr_right);} // search for $ arr = array (, 0,-1 ); // This is a sequential query of function search (& $ arr, $ findVal) {$ flag = false; for ($ I = 0; $ I
$ Arr [$ middleIndex]) {binarySearch ($ arr, $ findVal, $ middleIndex + 1, $ rightIndex); // if it is less than the intermediate number, then look forward} else if ($ findVal <$ arr [$ middleIndex]) {binarySearch ($ arr, $ findVal, $ leftIndex, $ middleIndex-1 );} else {echo "find this number. The subscript is $ middleIndex ";}}?>
I hope the sorting algorithm and Search Algorithm examples described in this article will be helpful for php programming.
Summary This article summarizes common php sorting algorithms and searches, which provide a good reference for algorithm design. I will share it with you for your reference...