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/>";