Some array sorting methods in PHP share _php skills

Source: Internet
Author: User
A. Internal sorting (directly loaded into memory for sorting): Includes interchange sort (bubbling and Quick Method), select Sort, insert sort
B. External ordering (because of the large amount of data that needs to be sorted by external storage): Include merge sort, direct merge sort

"Bubble sort: from backward forward, sequentially compare the sorting code of adjacent elements, if found in reverse order is exchanged, after the end of the round, another round, until all the adjacent numbers are not reverse, that is, in sequence."
Copy Code code as follows:

function Maopao ($arr, $style)//"The default delivery is a value, not an address. If you add an ampersand before the $arr, you point to the same address as the $ARR1, and the $arr1 outside the function is lined up.
{
$temp = 0;
$flag =false;
For ($i =0 $i <count ($arr)-1; $i + +)
{
for ($j =0; $j <count ($arr) $i; $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 transverse loop down the flag==false, indicating that each adjacent element in the longitudinal cycle is more than a large hour if conditions are not satisfied, that is, from small to large has been lined up, no need for a horizontal cycle
}
}
foreach ($arr as $key => $value)
{
echo $value. ', ';
}
}
$arr 1=array (101,101,-9,-8,0,76,1,57,43,90,23,-56);
Maopao ($arr 1, ' STB ');//small to Big

"Select the sort: The second number to the nth number is compared with the first number, Exchange, the third number to the number of n is compared with the second number, to exchange, until the end of the row."
Copy Code code as follows:

function Selectsort ($arr, $style)
{
$temp = 0;
$flag =false;
For ($i =0 $i <count ($arr)-1; $i + +)
{
for ($j = $i +1; $j <count ($arr); $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. ', ';
}
}
$arr 1=array (21.5,33,90,7,-4,5,55,11);
Selectsort ($arr 1, ' STB ');

Copy Code code as follows:

function Selectsort ($arr, $style)
{
$temp = 0;
$flag =false;
for ($i =0; $i <count ($arr)-1; $i + +)
{
for ($j = $i +1; $j <count ($arr); $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. ', ';
}
}
$arr 1=array (21.5,33,90,7,-4,5,55,11);
Selectsort ($arr 1, ' STB ');
echo "<br/>";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.