php中的一些数组排序方法分享
A.内部排序(直接加载到内存进行排序):包括交换式排序(冒泡和快速法)、选择式排序、插入式排序
B.外部排序(因数据量大,需借助外部存储进行排序):包括合并排序、直接合并排序
【冒泡排序:从后向前,依次比较相邻元素的排序码,若发现逆序则交换,一轮结束后,再来一轮,直到所有相邻数无逆序,即按顺序排完】
复制代码 代码如下:
function maoPao($arr,$style)//【默认传递的是值,不是地址。如果在$arr前加个&,则和$arr1指向同一个地址,函数外的$arr1也被排好了】
{
$temp=0;
$flag=false;
for($i=0;$i
for($j=0;$j
if($style=='bts') $op=$arr[$j]else if($style=='stb') $op=$arr[$j]>$arr[$j+1];
if($op)
{
$temp=$arr[$j];
$arr[$j]=$arr[$j+1];
$arr[$j+1]=$temp;
$flag=true;
}
}
if($flag==false)
{
break;//当一次横向循环下来flag==false;说明纵向循环里每次相邻元素比较大小时if条件均不满足,即从小到大已排好,无需再横向循环
}
}
foreach ($arr as $key => $value)
{
echo $value.',';
}
}
$arr1=array(101,101,-9,-8,0,76,1,57,43,90,23,-56);
maoPao($arr1,'stb');//small to big
【选择排序:第二个数至第n个数分别与第一个数比较,进行交换,第三个数至第n个数分别与第二个数比较,进行交换,直到排完】
复制代码 代码如下:
function selectSort($arr,$style)
{
$temp=0;
$flag=false;
for($i=0;$i
for($j=$i+1;$j
if($style=='bts') $op=$arr[$i]else if($style=='stb') $op=$arr[$i]>$arr[$j];
if($op)
{
$temp=$arr[$i];
$arr[$i]=$arr[$j];
$arr[$j]=$temp;
$flag=true;
}
}
if($flag==false)
{
break;
}
}
foreach ($arr as $key => $value)
{
echo $value.',';
}
}
$arr1=array(21.5,33,90,7,-4,5,55,11);
selectSort($arr1,'stb');
复制代码 代码如下:
function selectSort($arr,$style)
{
$temp=0;
$flag=false;
for($i=0;$i
for($j=$i+1;$j
if($style=='bts') $op=$arr[$i]else if($style=='stb') $op=$arr[$i]>$arr[$j];
if($op)
{
$temp=$arr[$i];
$arr[$i]=$arr[$j];
$arr[$j]=$temp;
$flag=true;
}
}
if($flag==false)
{
break;
}
}
foreach ($arr as $key => $value)
{
echo $value.',';
}
}
$arr1=array(21.5,33,90,7,-4,5,55,11);
selectSort($arr1,'stb');
echo "
";

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The best currency trading platform: Binance: The world's largest, with many transaction pairs, low fees and advanced functions. OKX: The derivatives trading platform is excellent, with low fees and friendly interface. Gate.io: There are many token choices, advanced trading options, and low fees. Bitget: One-click follow-up trading and quantitative strategy innovation.

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

Binance, OKX and Gate.io ranked in the top three. These platforms have their own advantages in transaction fees, security, transaction liquidity, products and services, and customer support.

The top ten most recommended digital currency exchanges in 2025: Binance: The world's largest exchange, offering a variety of trading pairs and derivatives. OKX: Focus on contract trading and derivatives, with a variety of innovative products. Gate.io: An established exchange that supports a wide range of trading pairs and investment options. Bitget: Perpetual contract and option trading platform, high leverage and low fees. Huobi Global: a well-established exchange in China, with a variety of trading varieties and financial products. Bybit: Expert in crypto derivatives trading, high leverage and low latency. KuCoin and so on.
