PHP Bubble Sorting algorithm implementation Code _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
$arr = Array (345,4,17,6,52,16,58,69,32,8,234);
for ($i =1; $i For ($j =count ($arr)-1; $j >= $i; $j-) {
if ($arr [$j]< $arr [$j-1]) {
$temp = $arr [$j-1];
$arr [$j-1] = $arr [$j];
$arr [$j] = $temp;
}
}
}

Basic Concepts
The basic concept of bubble sorting is to compare adjacent two numbers in turn, place decimals in front, and large numbers on the back. That is, first compare the 1th and 2nd numbers, put the decimals before the large number. Then compare the 2nd and 3rd numbers, place the decimal before the large number, and then continue until you compare the last two numbers, put the decimals before the decimal, and put the large number behind. Repeat the above process, still starting from the first logarithm comparison (because it may be due to the 2nd and 3rd number of exchanges, so that the 1th number is no longer greater than the 2nd number), the decimal place before, after the large number of places, has been compared to the minimum number of pairs of neighbors, the decimal place before the large number, the second end, Get a new decimal in the penultimate number. So go on until the final sort is finished.
Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, 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. The outer loop repeats 9 times, and the inner loop repeats 9,8,...,1 times sequentially. Each of the two elements of the comparison is related to the inner loop j, they can be identified by a[j] and a[j+1], the value of I sequentially,..., 9, for each I, the value of J is in sequence,... 10-i.

Produce
In many programming, we need to sort a sequence of numbers to facilitate statistics, the common sort method is bubble sort, binary tree sort, select sort and so on. And bubble sort has been favored because of its concise thinking method and relatively high efficiency.

Sorting Process
Imagine the sorted array r[1: N] vertically erected, each data element as a weight of the bubble, according to the light bubbles can not under the principle of heavy bubbles, from the bottom up scan array r, where scanning to violate the principle of light bubbles, so that it "floating upward", so repeated, until the last two bubbles are light in the upper, heavy in the next.

Update 2009-8-18: Error updating code.

http://www.bkjia.com/PHPjc/322346.html www.bkjia.com true http://www.bkjia.com/PHPjc/322346.html techarticle Copy the code as follows: $arr = Array (345,4,17,6,52,16,58,69,32,8,234), for ($i =1; $icount ($arr); $i + +) {for ($j =count ($arr)-1; $j = $i, $j-) {if ($arr [$j] $arr [$j-1]) {$temp = $arr [$j-1] ...

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