Php bubble sorting, quick sorting, quick search, two-dimensional array deduplication instance sharing

Source: Internet
Author: User
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!

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.