Bubble sort PHP Implementation Java bubble sort bubble Sort method bubble sort C language generation

Source: Internet
Author: User
/** * Bubble sort: stable, time complexity O (n^2) * Bubble Sort method is the simplest sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter, and thus upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times. * The so-called one-time processing, is to check the sequence from the bottom up, and always note the sequence of two adjacent elements is correct. * If two adjacent elements are found to be in the wrong order, then the "light" elements are below, exchanging their positions. * Obviously, after processing, the "lightest" element floats to the highest position, and after two times, the "light" element floats to the next high position. * In the second pass, you do not have to check because the element at the highest position is already the lightest element. * Generally, when I pass the processing, I do not have to check the high position above the elements, because the previous i-1 through the processing, they have been properly sequenced. * @param Array $arr * @return Array */ functionbubblesort(array $arr){$count= Count ($arr);if($count>1){//means to run N times for($i=0;$i<$count;$i++){//Indicates the number of times to compare each trip//After comparison, the largest number is on the far right and can be compared less once, so subtract $ for($j=$i+1;$j<$count-$i-1;$j++){if($arr[$j] >$arr[$j+1]){$temp=$arr[$j];$arr[$j] =$arr[$j+1];$arr[$j+1] =$temp; }            }        }    }Else{return$arr; }}

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the bubble sort of PHP implementation, including the bubble sort, PHP content, I hope the PHP tutorial interested in a friend helpful.

  • 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.