search

When I went to Kugou for an interview yesterday, in the written test, there was a programming question that asked me to use PHP to implement bubble sorting. Because I haven’t used bubble sorting for a long time, I forgot the principle of the algorithm, and the result was left blank. , really speechless. So today I will record the PHP bubble sorting code:

<code><span><?php </span><span>/**
 * 冒泡排序
 *<span> @param</span> array $numbers 要排序的数组,只限数字一维数组
 *<span> @param</span> boolean $asc   排序顺序,true是正序,false是逆序
 */</span><span><span>function</span><span>bubble_sort</span><span>(array <span>$numbers</span>, <span>$asc</span> = true)</span> {</span><span>$n</span> = count(<span>$numbers</span>);
    <span>// 外循环最多排(n - 1)次</span><span>$out_loop_cnt</span> = <span>$n</span> - <span>1</span>;
    <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> $out_loop_cnt</span>; <span>$i</span>++) {
        <span>// 内循环最多排(n - i - 1)次</span><span>$in_loop_cnt</span> = <span>$n</span> - <span>$i</span> - <span>1</span>;
        <span>for</span> (<span>$j</span> = <span>0</span>; <span>$j</span> $in_loop_cnt; <span>$j</span>++) {
            <span>// 根据排序顺序判断相邻两个数是否符合交换条件</span><span>$swap</span> = <span>$asc</span> ? (<span>$numbers</span>[<span>$j</span>] > <span>$numbers</span>[<span>$j</span> + <span>1</span>])
                : (<span>$numbers</span>[<span>$j</span>] $numbers[<span>$j</span> + <span>1</span>]);
            <span>if</span> (<span>$swap</span>) {
                <span>$temp</span> = <span>$numbers</span>[<span>$j</span> + <span>1</span>];
                <span>$numbers</span>[<span>$j</span> + <span>1</span>] = <span>$numbers</span>[<span>$j</span>];
                <span>$numbers</span>[<span>$j</span>] = <span>$temp</span>;
            }
        }
    }

    <span>return</span><span>$numbers</span>;
}

<span>$arr</span> = [<span>1</span>, <span>3</span>, <span>5</span>, <span>8</span>, <span>4</span>];

var_dump(bubble_sort(<span>$arr</span>));
<span>/**
 * 输出:
 * array (size=5)
 * 0 => int 1
 * 1 => int 3
 * 2 => int 4
 * 3 => int 5
 * 4 => int 8
 */</span>var_dump(bubble_sort(<span>$arr</span>, <span>false</span>));
<span>/**
 * 输出:
 * array (size=5)
 * 0 => int 8
 * 1 => int 5
 * 2 => int 4
 * 3 => int 3
 * 4 => int 1
 */</span></code>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced bubble sorting in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

PHP中int类型转换为字节的方法详解PHP中int类型转换为字节的方法详解Mar 06, 2024 pm 06:18 PM

PHP中int类型转换为字节的方法详解在PHP中,我们经常需要将整数类型(int)转换为字节(Byte)类型,比如在处理网络数据传输、文件处理或者加密算法等场景中。本文将详细介绍如何将int类型转换为字节类型,以及提供具体的代码示例。1.int类型与字节的关系在计算机领域,基本数据类型int表示整数,而字节(Byte)是计算机存储单位,通常是8位二进制数据

C++程序将double类型的变量转换为int类型C++程序将double类型的变量转换为int类型Aug 25, 2023 pm 08:25 PM

在C++中,int类型的变量只能保存正整数或负整数值;它们不能保存小数值。有float和double值可用于此目的。为了存储小数点后最多七位的小数,创建了双精度数据类型。整数到双精度数据类型的转换可以由编译器自动完成(称为“隐式”转换),也可以由程序员向编译器显式请求(称为“显式”转换)。在接下来的部分中,我们将介绍各种转换方法。隐式转换编译器自动执行隐式类型转换。要实现这一点,需要两个变量——一个是浮点类型,另一个是整数类型。当我们简单地将浮点值或变量分配给整数变量时,编译器将处理所有其他事情

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

int32的取值范围是多少int32的取值范围是多少Aug 11, 2023 pm 02:53 PM

int32的取值范围是从-2的31次方到2的31次方减1,即-2147483648到2147483647。int32是有符号的整数类型,意味着它可以表示正数、负数和零,它使用1位来表示符号位,而剩余的31位用来表示数值。由于一位被用来表示符号位,所以int32的有效位数是31位。

java int 是几位java int 是几位Mar 06, 2023 pm 04:09 PM

在java中,int是32位有符号数据类型,其变量需要32位内存;int数据类型的有效范围为-2147483648至2147483647,此范围中的所有整数称为整数字面量。一个整数字面量可以分配给一个int变量,例如“int num1 = 21;”。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment