PHP array bubble Sort algorithm instance _php skill

Source: Internet
Author: User

An example of this article describes the PHP array bubble sort algorithm. Share to everyone for your reference, specific as follows:

<?php
/*@ Bubble sorting algorithm
*
/$array =array (5,45,22,11,32,28,35,56,17,21,92);
$len =count ($array);//compute array length
for ($i =0 $i < $len-1; $i + +) {//need to compare $len-1 wheel, each round needs to compare $len-1 for
  ($j =0; $j <$ Len-1 $j + +) {//need to compare $len-1 times, because the loop to the last number, there are no numbers to compare, so the loop to the penultimate number is just
   $k = $j +1;//to get the next number of the current number of subscript, we compare the array subscript is 0-1, respectively, 1-2,3-4 Numeric Pairs
   if ($array [$j]> $array [$k]) {//Compare two number, if the previous number is larger than the latter one, the order of two numbers is
     $t = $array [$j];
     $array [$j]= $array [$k];
     $array [$k]= $t;
   } After the first cycle is compared, the next round of comparison
  }
}
print_r ($array);
/* Understanding the key to bubble sorting is that its comparison results are large numbers back, in turn, the largest number, the second largest number, the third largest number ... By analogy/
?>

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.