Share some array sorting methods in php

高洛峰
Release: 2023-03-02 15:48:02
Original
996 people have browsed it

A. Internal sorting (directly loaded into memory for sorting): including exchange sorting (bubble and quick methods), selective sorting, and insertion sorting
B. External sorting (due to the large amount of data, external storage is required for sorting ): Including merge sort, direct merge sort

[Bubble sort: From back to front, compare the sort codes of adjacent elements in sequence. If a reverse order is found, swap them. After one round, another round will be done until all adjacent elements are found. No reverse order, that is, arranged in order]
Copy code The code is as follows:
function maoPao($arr,$style)//[The default value is passed, not the address. If you add an & before $arr, it points to the same address as $arr1, and $arr1 outside the function is also arranged]
{
$temp=0;
$flag=false;
for($i=0 ;$i{
for($j=0;$j{
if($style= ='bts') $op=$arr[$j]<$arr[$j+1];
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;//When a horizontal cycle comes down, flag==false; means that each adjacent time in a vertical cycle When comparing the sizes of elements, the if conditions are not met, that is, they have been arranged from small to large, and there is no need to loop horizontally
}
}
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

【Selection sorting: The second to nth numbers are compared with the first number respectively and exchanged, and the third to nth numbers are compared with the second number respectively and exchanged until Finished]
Copy code The code is as follows:
function selectSort($arr,$style)
{
$temp=0;
$flag=false;
for($i=0;$i{
for($j=$i+1;$j{
if($style=='bts') $op=$arr[$ i]<$arr[$j];
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');

Copy code The code is as follows:
function selectSort($arr,$style)
{
$temp =0;
$flag=false;
for($i=0;$i{
for($j=$i+1;$j{
if($style=='bts') $op=$arr[$i]<$arr[$j];
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 "
";

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!