This article mainly introduces php bubble sorting, fast sorting, fast searching, and two-dimensional array deduplication instance sharing. For more information, see the next section.
// Bubble sorting function bubble_sort ($ array) {$ count = count ($ array); if ($ count <= 0) {return false;} for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = 0; $ j <$ count-$ I-1; $ j ++) {if ($ array [$ j]> $ array [$ j + 1]) {$ temp = $ array [$ j]; $ array [$ j] = $ array [$ j + 1]; $ array [$ j + 1] = $ temp ;}} return $ array ;}
II. quick sorting
// Quick_sort ($ array) {$ count = count ($ array); if ($ count <= 1) {return $ array ;} $ key = $ array [0]; $ array_left = array (); $ array_right = array (); for ($ I = 1; $ I <$ count; $ I ++) {if ($ array [$ I] <$ key) {$ array_left [] = $ array [$ I];} else {$ array_right [] = $ array [$ I] ;}}$ array_left = quick_sort ($ array_left); $ array_right = quick_sort ($ array_right); return array_merge ($ array_left, array ($ key), $ array_right) ;}$ myarray = array (, 8); print_r (bubble_sort ($ myarray); echo"
"; Print_r (quick_sort ($ myarray); echo"
";
3. quick search for the position where the value appears for the first time
/*** The location where the first query value appears * @ param array $ array * @ param string $ k the value to be searched * @ param int $ low the minimum key value in the search range * @ param int $ maximum key value in the high range */function search ($ array, $ k, $ low = 0, $ high = 0) {// determines whether it is the first time to call if (count ($ array )! = 0 and $ high = 0) {$ high = count ($ array);} // if there are still remaining array elements if ($ low <= $ high) {// take the middle value of $ low and $ high $ mid = intval ($ low + $ high)/2 ); // if it is found, if ($ array [$ mid] == k) {return $ mid;} // if it is not found, continue to search for elseif ($ k <$ array [$ mid]) {return search ($ array, $ k, $ low, $ mid-1 );} else {return search ($ array, $ k, $ mid + 1, $ high) ;}} return-1 ;}$ array = array (, 9, 10, 8); // test the search function echo search ($ array, 8); // call the search function and output the search result.
4. remove two-dimensional array duplicates
/*** Remove repeated items in a two-dimensional array * @ param $ array2D array * @ param $ the key corresponding to the field when the keyArray is restored * @ return array removes the array of repeated items */public function array_unique_fb ($ array2D, $ keyArray) {$ temp = array (); foreach ($ array2D as $ v) {$ v = join (",", $ v); // dimension reduction, you can also use implode to convert a one-dimensional array to a comma-connected string $ temp [] = $ v;} $ temp = array_unique ($ temp); // remove duplicate strings, that is, the repeated one-dimensional array foreach ($ temp as $ k => $ v) {// $ temp [$ k] = explode (",", $ v ); // re-assemble the split array $ temp [$ k] = array_combine ($ keyArray, explode (",", trim ($ v )));} return $ temp;} $ testArray = array_unique_fb (array ('a' => 1, 'B' => 2, 'C' => 3 ), array ('a' => 1, 'B' => 2, 'C' => 3), array ('a' => 1, 'B' => 2, 'C' => 3), array ('A', 'B', 'C'); print_r ($ testArray );
For more articles about php bubble sorting, quick sorting, quick search, and two-dimensional array deduplication, please follow the PHP Chinese network!