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

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

Jun 13, 2016 am 11:59 AM
phppayMemoryinternalbubbleshareloadandfastsortarraymethodconduct

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!