Php basic sorting-PHP source code

Source: Internet
Author: User
Php basic sorting

[PHP] code
 $ Arr [$ I]) {$ temp = $ arr [$ i-1]; $ arr [$ i-1] = $ arr [$ I]; $ arr [$ I] = $ temp; $ swapped = true ;}}while ($ swapped); return $ arr ;}// select the sort function selectSort ($ arr) {$ minimumPosition = 0; $ len = count ($ arr); for ($ I = 0; $ I <$ len-1; $ I ++) {for ($ j = $ I + 1; $ j <$ len-$ I; $ j ++) {if ($ arr [$ j] <$ arr [$ minimumPosition]) {$ minimumPosition = $ j ;}$ temp = $ arr [$ I]; $ arr [$ I] = $ arr [$ minimumPosition]; $ arr [$ minimumPosition] = $ temp;} return $ arr;} // Insert the sorting function insertSort1 ($ arr) {$ len = count ($ arr); for ($ I = 1; $ I <$ len; $ I ++) {$ extractElement = $ arr [$ I]; for ($ j = $ I-1; $ j> = 0; $ j --) {if ($ arr [$ j]> $ extractElement) {$ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ extractElement ;}} return $ arr ;} /*** insert sort ** @ param Array $ a unordered set * @ return Array ordered set */function insertSort2 ($ a) {$ temp; $ I; $ j; $ size_a = count ($ a); # for ($ I = 1; $ I <$ size_a; $ I ++) starting from the second element) {if ($ a [$ I] <$ a [$ i-1]) {$ j = $ I; # save the location of the current element $ temp = $ a [$ I]; # value of the current element # Move the element to the right if it is smaller than itself, otherwise insert the element to the current position while ($ j> 0 & $ temp <$ a [$ j-1]) {$ a [$ j] = $ a [$ j-1]; $ j --;} # insert element $ a [$ j] = $ temp;} return $ ;} /*** obtain the random number * @ param Integer $ size quantity * @ return Integer */function randomNumber ($ size = 10) {$ rand = array (); srand (time (NULL); for ($ I = 0; $ I <$ size; $ I ++) {array_push ($ rand, mt_rand ));} return $ rand;} // print_r (implode (",", bubbleSort ($ unsortList); // print_r (implode (",", selectSort ($ unsortList); // print_r (implode (",", insertSort1 ($ unsortList); // print_r (implode (",", insertSort2 ($ unsortList )));

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.