Php Bubble sort (Bubble sort) algorithm detailed

Source: Internet
Author: User

Objective

The bubble sort probably means to compare the adjacent two numbers in turn, then sort by size until the last two digits. 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. But in fact in the actual process can also be in accordance with their own needs in turn, the big tree forward, the decimal place.

Actual combat

Directly on the code:

<?php/** * Bubble Sorting algorithm example *///here is a one-dimensional array to demonstrate $demo_array = Array (23,15,43,25,54,2,6,82,11,5,21,32,65);// The first-level for loop can be understood as starting from the array key to 0 to start the loop to the last for ($i =0; $i <count ($demo _array); $i + +) {    //The second layer loops from the place where the key is $i to the end of the array for    ($j = $i + 1; $j <count ($demo _array), $j + +) {        //Compare the size of adjacent two values in the array        if ($demo _array[$i] > $demo _array[$j]) {            $tmp            = $demo _array[$i];//tmp Here is a temporary variable            $demo _array[$i] = $demo _array[$j];//first change position            $demo _array[$j] = $tmp;            Complete position Interchange        }    }}//print result set echo ' <pre> '; Var_dump ($demo _array); Echo ' </pre> ';

Operation Result:

Array (+) {  [0]=>  int (2)  [1]=>  Int (5)  [2]=>  Int (6)  [3]=>  Int (one)  [4]=>  int (  [5]=> int)  [6]=>  int  (All) [7]=> Int (+)  [8]=>  Int (+)  [9]=>  Int ()  [10]=>  int (si)  [11]=>  Int (+)  [12]=>  Int (82)}

From the results above, we can see that the order of the key values in the array has been changed and the ordering is successful.

If the above algorithm is to the array of key values according to the size of the value from small to large to sort, then vice versa from large to small how to do?

Very simple, just modify a comparison symbol can be, as follows:

<?php/** * Bubble Sorting algorithm example *///here is a one-dimensional array to demonstrate $demo_array = Array (23,15,43,25,54,2,6,82,11,5,21,32,65);// The first-level for loop can be understood as starting from the array key to 0 to start the loop to the last for ($i =0; $i <count ($demo _array); $i + +) {    //The second layer loops from the place where the key is $i to the end of the array for    ($j = $i + 1; $j <count ($demo _array), $j + +) {        //Compare the size of adjacent two values in the array        if ($demo _array[$i] < $demo _array[$j]) {            $tmp            = $demo _array[$i];//tmp Here is a temporary variable            $demo _array[$i] = $demo _array[$j];//first change position            $demo _array[$j] = $tmp;            Complete position Interchange        }    }}//print result set echo ' <pre> '; Var_dump ($demo _array); Echo ' </pre> ';

Operation Result:

Array (+) {  [0]=>  int ([1]=>) int (+  )  [2]=> int (si  )  [3]=>  Int (43)  [4]=>  Int (+)  [5]=>  Int (21)  [6]=>  int (  [7]=>  int)  [8]=>  Int ([9]=>) int (one  )  [10]=> Int (  6)  [11]=>  Int (5)  [12]=>  int (2)}

So it's easy to change the order.

Extended

If you look closely at the above code, you will find that there is a place to pay attention, that is, swap variables worth the place. Yes, this is the core point of bubbling, this technique is mastered and can be used elsewhere.

Here we'll talk a little bit about this.

Principle:

There are now two variables for A and B, and the requirement is to swap their values.

See the topic, we may first think of the direct assignment, but if the direct assignment, regardless of who is assigned to whom, one must be overwritten, so we can come up with a third variable C, temporarily store A or B value, so that we can achieve the demand target.

$c = $a; Staged $ A = $b; b to a$b = $c; The staged a value is given to B

Note: Actually does not need the third variable, also can achieve the interchange a, the B variable value, may use substr (), Str_replace () and so on method, here because is introduces the bubble sort, therefore does not extend too much.

Summarize

On the bubble sort of so much, summed up, mainly is two points:

Cycle comparison

Exchange Key value

Can complete these two points, basically OK, of course, about the bubble sorting algorithm there are many, here is only one, interested students can study under their own.

  • Related Article

    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.