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