PHP Bubble Sort Method

Source: Internet
Author: User

The basic idea of the bubble sorting method is that every time a cycle is passed, the maximum exchange is made to the last, and finally it becomes an ordered array from small to large;

Small cases: from small to large, and then from big to small;

<?php$arr1=array (1,-1,10,-9,0,3,7,-20,100), function Sort1 ($arr) {for ($i =0; $i <count ($arr)-1; $i + +) {for ($j = 0;        $j <count ($arr)-$i; $j + +) {if ($arr [$j]> $arr [$j +1]) {$temp = $arr [$j];        $arr [$j]= $arr [$j +1]; $arr [$j +1]= $temp; }}}return $arr;}  $BRR =sort1 ($arr 1);p rint_r ($BRR); $j =0;for ($i =count ($BRR)-1; $i >=0; $i-) {$CRR [$j]= $brr [$i]; $j + +;} Print_r ($CRR);? >
[[email protected] html]# php sort1.php Array ([0] = -20 [1] = 9 [2] +-1 [3] = 0 [4] => ;    1 [5] = 3 [6] = 7 [7] = [8] = +) Array ([0] = [1] = [2] = 7 [3] = 3 [4] = 1 [5] = 0 [6] +-1 [7] +-9 [8] =-20)


This article is from the "Progress 1% per day" blog, please be sure to keep this source http://jackyan.blog.51cto.com/2589874/1545937

PHP Bubble Sort Method

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.