Php array bubble sorting algorithm instance, php array algorithm instance _ PHP Tutorial

Source: Internet
Author: User
Php array bubble sorting algorithm instance and php array algorithm instance. Php array bubble sorting algorithm example, php array algorithm example this article describes the php array bubble sorting algorithm. For your reference, see php * @ bubble sorting algorithm php array bubble sorting algorithm instance and php array algorithm instance.

This article describes the php array bubble sorting algorithm. We will share this with you for your reference. The details are as follows:

<? Php/* @ bubble sorting algorithm */$ array = array (,); $ len = count ($ array ); // calculate the array length for ($ I = 0; $ I <$ len-1; $ I ++) {// you need to compare $ len-1 wheel, each round needs to compare $ len-1 times for ($ j = 0; $ j <$ len-1; $ j ++) {// needs to compare $ len-1 times, because no number can be compared after the loop to the last number, the number of loops to the second to the last is exactly $ k = $ j + 1; // Obtain the subscript of the next number of the current number, we compare the if ($ array [$ j]> $ array [$ k]) values with the subscript 0---4, respectively. {// compare two numbers. if the previous number is greater than the last one, the order of the two numbers is exchanged. $ t = $ array [$ j]; $ array [$ j] = $ array [$ k]; $ array [$ k] = $ t;} // after the first loop comparison, perform the next round of comparison. } Print_r ($ array);/* the key to understanding the bubble sorting is that its comparison result is to put a large number in the future, and the largest and second largest number is obtained in sequence, the third largest number... And so on */?>

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.