PHP algorithm-bubble

Source: Internet
Author: User
<?php  //php algorithm bubble//3, 8, 99, 75, 23, 1, 29, 51, 24, 13, 78.//the first cycle, if there are small elements on the back, that is, bubbling, so the cycle, the smallest 1 bubble to the top of the , that is, the second cycle of $array[10]=1;//, if there are small elements on the back, and bubbling, so that the cycle, the second small 3 bubbles to the penultimate position, that is, $array[9]=3;//... And so on The number of traversal loop array-1 times, according to the order from the big to the minor array,//which can do some optimization, the first bubble needs to bubble to the last element, the second bubble to the penultimate element is OK, that is, COUNT ($array)-2, can also be optimized place here does not do in-depth discussion. function Dump ($arr) {echo ' <pre> ';p rint_r ($arr); Echo ' </pre> ';} $array  =  Array (3,8,99,75,23,1,29,51,24,13,78);d UMP ($array), for ($i =0; $i <count ($array); $i + +) {for ($j =0 ; $j <count ($array)-$i-1; $j + +) {if ($array [$j]< $array [$j +1]) {$temp = $array [$j]; $array [$j] = $array [$j +1]; $array [$j +1] = $temp;}}} Dump ($array);

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.