Bubble sort-php, bubbling-php_php tutorial

Source: Internet
Author: User

Bubble sort-php, bubbling-php


the code implemented by PHP is first:

function
Bubble_sort ($array) { for($i= 0;$i<Count($array)-1;$i++) {//$i is the number of elements that have been ordered for($j= 0;$j<Count($array)-1-$i;$j++) {//$j is the number of elements that need to be sorted, minus $i by total length if($array[$j] >$array[$j+ 1]) {//Sort by Ascending $temp=$array[$j]; $array[$j] =$array[$j+ 1]; $array[$j+ 1] =$temp; } } } return $array;}

$a = Array (5, 1, 4, 7);

Code Execution Process:
i = 0;
j = 0;
if ($arr [0] > $arr [1] = 5 > 1 conditions set, swap position, form a new array = 1 5 4 7 J + +
if ($arr [1] > $arr [2]) = 5 > 4 condition established, swap position, form new array = 1 4 5 7
if ($arr [2] > $arr [3] = 5 > 7 condition not set, array remains unchanged, 1 4 5 7 J + + J=3 exit inner Loop, i++






http://www.bkjia.com/PHPjc/934724.html www.bkjia.com true http://www.bkjia.com/PHPjc/934724.html techarticle bubble sort-php, bubbling-php PHP implementation Code First: function Bubble_sort ($array) {for ($i = 0; $i count ($array)-1; $i + +) {//$i as already Number of ordered elements ...

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