A. Internal sorting (directly loaded into memory for sorting): including exchange sorting (bubble and fast methods), selection sorting, insertion sorting
B. External sorting (due to the large amount of data, external storage is required Sorting): including merge sort, direct merge sort
[Bubble sort: From back to front, compare the sort codes of adjacent elements in sequence. If the reverse order is found, exchange it. After one round, another round will be done. Until all adjacent numbers are in reverse order, that is, they are arranged in order]
Copy the code The code is as follows:
function maoPao($ arr,$style)//[The value is passed by default, 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 loop flag==false; it means that the if condition is not satisfied every time the adjacent elements are compared in the vertical loop. That is, it has 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 sort: The second number to the nth number are compared with the first number respectively, and exchanged, and the third number to the nth number are compared. The n numbers are compared with the second number respectively and exchanged until the arrangement is completed]
Copy the 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 "
http://www.bkjia.com/PHPjc/325607.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325607.htmlTechArticleA. Internal sorting (directly loaded into memory for sorting): including exchange sorting (bubble and fast methods) , selective sorting, insertion sorting B. External sorting (due to the large amount of data, external sorting is required...