search
Homephp教程php手册PHP实现冒泡排序,php冒泡排序

PHP实现冒泡排序,php冒泡排序

1、首先我们必须弄清楚什么是冒泡排序,不理解冒泡排序的原理,我们就无法写出代码。

冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。至此第一趟结束,将最大的数放到了最后。在第二趟:仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再小于第2个数),将小数放前,大数放后,一直比较到倒数第二个数(倒数第一的位置上已经是最大的),第二趟结束,在倒数第二的位置上得到一个新的最大数(其实在整个数列中是第二大的数)。如此下去,重复以上过程,直至最终完成排序。

PHP实现代码:

<?<span>php
    
    </span><span>//</span><span>冒泡排序方法</span>

    <span>function</span> bubblesort(&<span>$arr</span><span>){
            </span><span>//</span><span>定义一个变量保存交换的值</span>
        <span>$temp</span> =0<span>;
        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$arr</span>);<span>$i</span>++<span>){
            
            </span><span>for</span>(<span>$j</span>=0;<span>$j</span><<span>count</span>(<span>$arr</span>)-<span>$i</span>-1;<span>$j</span>++<span>){
                        
                </span><span>if</span>(<span>$arr</span>[<span>$j</span>]><span>$arr</span>[<span>$j</span>+1<span>]){
                    </span><span>//</span><span>如果前面的那个数大于后面的那个数,那么他们就进行交换</span>
                    <span>$temp</span>=<span>$arr</span>[<span>$j</span><span>];
                    </span><span>$arr</span>[<span>$j</span>]=<span>$arr</span>[<span>$j</span>+1<span>];
                    </span><span>$arr</span>[<span>$j</span>+1]=<span>$temp</span><span>;
                }
            }            
        }
    }

    </span><span>$arr</span>=<span>array</span>(100,99,200,5,-4,6,-7<span>);
    bubbleSort(</span><span>$arr</span><span>);
    </span><span>print_r</span>(<span>$arr</span>);   <span>//</span><span>数组是值传递,所以传递的时候加个&符号就是地址传递,改变外部变量</span>


?>

 

 

 

Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft