1.
<?php
/**
* Fast sorting without space and saving time
*/
$arr =array (5,1,8,2,6,4,9,3,7);
$arr =array (1,2,3,4,5,6,7,8,9);
$arr =array (9,8,7,6,5,4,3,2,1);
$arr =array (1,5,9,7,6,4);
function Fast ($arr)
{
Statistic array length
$length =count ($arr);
Define two empty arrays
$left =array ();
$right =array ();
Returns an array if the array length is <1
if ($length <=1) {
return $arr;
}
for ($i =1; $i < $length; $i + +) {
Find a number as a comparison, in order to facilitate the first number in the array
if ($arr [0]> $arr [$i]) {
This array is less than this number, and the first time after the array is 1243
$left []= $arr [$i];
}else{
Put this array greater than this number, the first time after the array 8697
$right []= $arr [$i];
}
}
Var_dump ($left);
recursive invocation Implementation Ordering
$left =fast ($left);
Put the first comparison number in one of two arrays
$left []= $arr [0];
$right =fast ($right);
Return Array_merge ($left, $right);
}
Var_dump (Fast ($arr));
?>
2.
<?php
/**
* Bubble Sort Fee time
*/
Arbitrarily define an array, that is, the number to compare
$arr =array (12,11,63,9,25);
$arr =array (1,2,3,4,5,6,7,8,9);
$arr =array (999,888,777,6,5,4,3,2,1);
function Maopao ($arr)
{
The length of the statistic array
$length =count ($arr);
The outer loop is the number of arrays I'm going to traverse this array how many times, each time the inner loop sequence of the array
for ($i =0; $i < $length; $i + +)
{
The inner loop is for sorting, $l 1 is because the bubble sort method only compares $l-1 times
for ($j =0; $j < $length-1; $j + +)
{
If the number of neighbors is not smaller than the number behind it, you don't need to sort, if the number of small back appears in the previous position
if ($arr [$j]< $arr [$j +1]) {
$t = $arr [$j];
$arr [$j]= $arr [$j +1];
$arr [$j +1]= $t;
}
}
}
return $arr;
}
Var_dump (Maopao ($arr));
?>
3.
<?php
/**
* Bucket sorting fee space
*/
Defines an array, and the number to sort
$arr =array (3,5,5,2,8);
Function Tong ($arr)
{
Define a one-dimensional array, which must be larger than the number you want to compare, and the number in the array is equivalent to a bucket, defined within 100
$tong =range (0,100);
Initialize the number in the array to 0.
for ($i =0; $i <=10; $i + +) {
$tong [$i]=0;
}
To assign a value to an array again
foreach ($arr as $value) {
The number in the $arr corresponds to the index in $ A, and the index value of a $ A in the $arr is going to increment, and + + is the self-increasing meaning
$tong [$value]++;
}
for ($i =0; $i <=10; $i + +) {
$j =1 because the index value in the $a is >=1 what we want to output
for ($j =1; $j <= $tong [$i]; $j + +)
{
echo $i;
}
}
}
Tong ($arr);
?>
PHP Three sorting algorithms