Bubble Sort algorithm: PHP implementation and source code of bubble sort algorithm

Source: Internet
Author: User
Tags array sort variable

Basic concepts:
The basic concept of bubble sort (bubblesort) is to compare the number of adjacent two digits, place the decimal number in the front, and put the large numbers behind. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two, the number of decimal places before the large number of put. At the end of the first trip, the largest number was put to the end. On the second trip: the comparison is still starting from the first logarithm (since the exchange of the 2nd and 3rd numbers is possible, so that the 1th number is no longer less than the 2nd number, the decimal place before, after the large number, has been compared to the penultimate number (the first in the last position is already the largest), the second end, To get a new maximum number (in fact, the second largest number in the whole series) in the penultimate position. So go on, repeat the process until you finish sorting.
Because the decimal is always pushed forward in the sort process, the large number is placed backward, which is the equivalent of a bubble going up, so called bubble sort.
With the double loop, the outer loop variable is set to I and the inner loop variable is set to J. If there are 10 numbers that need to be sorted, the outer loop repeats 9 times, and the inner loop repeats 9,8,...,1 times. Each of the two elements to be compared is related to inner Loop j, which can be identified by a[j] and a[j+1 respectively, and the value of I is 1,2,..., 9, followed by 1,2 for each i,j value,... 10-i.
<?php
/*
* Point one:remember Swap variable
* Point two:remember inside loop size
*/
$arr = Array (2,1,4,2,7); Init a array variable
$result = Maosort ($arr); Invoke a function and get result
Echo ' <pre> '; Output format
Print_r ($result); Output result
Echo ' </pre> ';
function Maosort ($arr) {//function start
$size = count ($arr)-1; Need to loop the size is total length-1
for ($i = 0; $i < $size; $i + +) {//outside loop
for ($j = 0; $j < $size-$i; $j + +) {//inside loop
if ($arr [$j] < $arr [$j +1]) {//compare big or small
$temp = $arr [$j]; Start swap
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $temp;
}
}
}
return $arr;
}
?> This article links http://www.cxybl.com/html/wlbc/Php/20130504/37470.html

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.