Home php教程 php手册 php中的一些数组排序方法分享

php中的一些数组排序方法分享

Jun 13, 2016 am 11:59 AM
php pay Memory internal bubble share load and fast sort array method conduct

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 "
";

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Download the top ten exchange platforms in the currency circle. The summary of the four major currency circle trading platforms in 2025 Download the top ten exchange platforms in the currency circle. The summary of the four major currency circle trading platforms in 2025 Feb 20, 2025 pm 06:18 PM

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.

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

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.

The top ten virtual currency trading platforms in China are recommended. The top ten virtual currency trading platforms are the latest. The top ten virtual currency trading platforms in China are recommended. The top ten virtual currency trading platforms are the latest. Feb 27, 2025 pm 05:15 PM

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.

Top 10 Digital Currency Exchanges 2025 Recommended List of Leading Currency Trading Platforms Top 10 Digital Currency Exchanges 2025 Recommended List of Leading Currency Trading Platforms Feb 27, 2025 pm 06:00 PM

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.

See all articles