国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 Detailed explanation of PHP bubble sort (Bubble Sort) algorithm

Detailed explanation of PHP bubble sort (Bubble Sort) algorithm

Nov 11, 2016 am 09:26 AM

Foreword

Bubble sorting roughly means comparing two adjacent numbers in sequence, and then sorting according to size until the last two digits. Since in the sorting process, small numbers are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting. But in fact, in the actual process, you can also use it in reverse according to your own needs. The big trees are placed forward and the decimals are placed backward.

Actual combat

Go directly to the code:

<?php/**
 * 冒泡排序算法示例
 */// 這里以一維數(shù)組做演示$demo_array = array(23,15,43,25,54,2,6,82,11,5,21,32,65);// 第一層for循環(huán)可以理解為從數(shù)組中鍵為0開(kāi)始循環(huán)到最后一個(gè)for ($i=0;$i<count($demo_array);$i++) {    // 第二層將從鍵為$i的地方循環(huán)到數(shù)組最后
    for ($j=$i+1;$j $demo_array[$j]) {            $tmp            = $demo_array[$i]; // 這里的tmp是臨時(shí)變量
            $demo_array[$i] = $demo_array[$j]; // 第一次更換位置
            $demo_array[$j] = $tmp;            // 完成位置互換
        }
    }
}// 打印結(jié)果集echo &#39;&#39;;
var_dump($demo_array);echo &#39;&#39;;

Running result:

array(13) {
  [0]=>  int(2)
  [1]=>  int(5)
  [2]=>  int(6)
  [3]=>  int(11)
  [4]=>  int(15)
  [5]=>  int(21)
  [6]=>  int(23)
  [7]=>  int(25)
  [8]=>  int(32)
  [9]=>  int(43)
  [10]=>  int(54)
  [11]=>  int(65)
  [12]=>  int(82)
}

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

If the above algorithm is to sort the key values ????in the array from small to large according to the value size, then how to operate from large to small in reverse?

It’s very simple, just modify a comparison symbol, as follows:

<?php/**
 * 冒泡排序算法示例
 */// 這里以一維數(shù)組做演示$demo_array = array(23,15,43,25,54,2,6,82,11,5,21,32,65);// 第一層for循環(huán)可以理解為從數(shù)組中鍵為0開(kāi)始循環(huán)到最后一個(gè)for ($i=0;$i<count($demo_array);$i++) {    // 第二層將從鍵為$i的地方循環(huán)到數(shù)組最后
    for ($j=$i+1;$j<count($demo_array);$j++) {        // 比較數(shù)組中相鄰兩個(gè)值的大小
        if ($demo_array[$i] < $demo_array[$j]) {            $tmp            = $demo_array[$i]; // 這里的tmp是臨時(shí)變量
            $demo_array[$i] = $demo_array[$j]; // 第一次更換位置
            $demo_array[$j] = $tmp;            // 完成位置互換
        }
    }
}// 打印結(jié)果集echo &#39;&#39;;
var_dump($demo_array);echo &#39;&#39;;

Running result:

array(13) {
  [0]=>  int(82)
  [1]=>  int(65)
  [2]=>  int(54)
  [3]=>  int(43)
  [4]=>  int(32)
  [5]=>  int(25)
  [6]=>  int(23)
  [7]=>  int(21)
  [8]=>  int(15)
  [9]=>  int(11)
  [10]=>  int(6)
  [11]=>  int(5)
  [12]=>  int(2)
}

That’s it, change the order easily.

Extension

If you look closely at the above code, you will find that there is one place worth paying attention to, which is the place where swap variables are worth paying attention to. Yes, this is also the core point of bubbling. Once you master this technique, you can also use it in other places in the future.

Here we will talk about this a little bit.

Principle:

Now there are two variables A and B, and the requirement is to interchange their values.

When we see the title, we may first think of direct assignment, but if we assign directly, no matter who is assigned to whom first, one of them will definitely be overwritten. From this, we can come up with a third variable C, temporarily Store the value in A or B so that the required goal can be achieved.

$c = $a; // 暫存
$a = $b; // b給a
$b = $c; // 暫存的a值再給b


Note: In fact, there is no need for a third variable. It is also possible to interchange the values ??of A and B variables. You can use substr(), str_replace() and other methods. Since we are introducing bubble sorting here, so But it’s not too much of an extension.

Summary

There are so many things about bubble sorting. To sum up, there are two main points:

  • Loop comparison

  • Exchange key values

If you can complete these two points, it is basically OK. Of course, there are many other algorithms for bubble sorting. Here is just one of them. Interested students can study it on their own.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)