Some array sorting methods in PHP share _php tutorial

Source: Internet
Author: User
A. Internal sorting (directly loaded into memory for sorting): Includes Exchange sort (bubbling and fast method), select Sort, insert sort
B. External sorting (due to large amount of data, need to sort with external storage): Includes merge sort, direct merge sort

"Bubble sort: from the back forward, then compare the adjacent elements of the sorting code, if the reverse is found to exchange, after the end of the round, another round until all adjacent numbers are not reversed, that is, in order to complete the"
Copy CodeThe code is as follows:
function Maopao ($arr, $style)//"The default is to pass a value, not an address." If you add a &amp before the $arr, the $arr1 points to the same address, and the $arr1 outside the function is also queued.
{
$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 transverse cycle down the flag==false, indicating that each of the adjacent elements in the longitudinal cycle is relatively large hours if the condition is not satisfied, that is, from small to large has been lined up, no further transverse 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 Sort: The second number to the nth number is compared with the first number, and the third number to the nth number is compared with the second number, which is exchanged until the "
Copy CodeThe 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. ', ';
}
}
$arr 1=array (21.5,33,90,7,-4,5,55,11);
Selectsort ($arr 1, ' STB ');

Copy CodeThe 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. ', ';
}
}
$arr 1=array (21.5,33,90,7,-4,5,55,11);
Selectsort ($arr 1, ' STB ');
echo "
";

http://www.bkjia.com/PHPjc/325607.html www.bkjia.com true http://www.bkjia.com/PHPjc/325607.html techarticle A. Internal sorting (directly loaded into memory for sorting): Includes Exchange sort (bubbling and fast method), select Sort, insert sort B. External sorting (due to the large amount of data, need to use external ...

  • Related Article

    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.