Common PHP Algorithms: Bubble sorting, fast sorting, insert sorting, trade-off sorting, and binary search ,.]

Source: Internet
Author: User
Common PHP Algorithms: Bubble sorting, fast sorting, insert sorting, selection sorting, binary search ,..] bubble sorting functionbubblesort ($ arr) {for ($ i0, $ jcount ($ arr); $ I & lt; $ j; $ I ++) {for ($ k $ J-1; $ k & gt; $ I; $ k --) {PHP common algorithms [bubble sort, fast sort, insert sort, select sort, binary search ,..]
// Bubble sorting function bubblesort ($ arr) {for ($ I = 0, $ j = count ($ arr); $ I <$ j; $ I ++) {for ($ k = $ J-1; $ k> $ I; $ k --) {if ($ arr [$ k] <$ arr [$ k-1]) list ($ arr [$ k-1], $ arr [$ k]) = array ($ arr [$ k], $ arr [$ k-1]);} return $ arr;} $ arr = array (123,); print_r (bubblesort ($ arr ));
// Quick sorting function quicksort ($ arr) {if ($ count = count ($ arr) <= 1) return $ arr; $ base = $ arr [0]; $ left = $ right = array (); for ($ I = 1; $ I <$ count; $ I ++) {if ($ arr [$ I] <= $ base) $ left [] = $ arr [$ I]; else $ right [] = $ arr [$ I];} $ left = quicksort ($ left); $ right = quicksort ($ right); return array_merge ($ left, array ($ base), $ right);} echo join (', ', quicksort (array (1, 3, 65, 6 )));?
// Insert the sorting function insertsort ($ arr) {for ($ I = 1, $ j = count ($ arr); $ I <$ j; $ I ++) {$ k = $ I; while ($ k> 0 & $ arr [$ k-1]> $ arr [$ k]) {list ($ arr [$ k], $ arr [$ k-1]) = array ($ arr [$ k-1], $ arr [$ k]); $ k -- ;}} return $ arr ;} $ array = array (,); print_r (insertsort ($ array ));

?

// Select sorting, (non-recursive) function selectsort ($ arr) {for ($ I = 0, $ j = count ($ arr); $ I <= $ j; $ I ++) {$ min = $ I; $ temp = $ arr [$ I]; for ($ k = $ I + 1; $ k <$ j; $ k ++) {if ($ temp> $ arr [$ k]) {$ min = $ k; $ temp = $ arr [$ k] ;}} if ($ min! = $ I) list ($ arr [$ min], $ arr [$ I]) = array ($ arr [$ I], $ arr [$ min]);} return $ arr;} $ arr = array (9, 3, 11, 23, 90, 99, 12, 34, 22, 87, 32); print_r (selectsort ($ arr ));

?

// Select sorting (recursive implementation) function selectsort2 ($ arr, $ start = 0) {if ($ count = count ($ arr) = $ start + 1) return $ arr; $ new = array (); $ min = $ arr [$ start]; $ min_index = $ start; for ($ I = $ start + 1; $ I <$ count-1; $ I ++) {if ($ arr [$ I] <$ min) {$ min = $ arr [$ I]; $ min_index = $ I;} if ($ arr [$ start]! = $ Min) list ($ arr [$ start], $ arr [$ min_index]) = array ($ arr [$ min_index], $ arr [$ start]); return selectsort ($ arr, $ start + 1);} $ arr = array (9, 3, 11, 23, 90, 99, 12, 34, 32); print_r (selectsort ($ arr ));
?

?

?

 $ Base) return binarysearch ($ arr, $ value, $ index + 1, $ end); else return $ index ;}$ arr = array (1, 3, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20); $ value = 8; echo binarysearch ($ arr, $ value );
?

To be continued...

Related Article

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.