Counting Sort
Counting sort is sorting algorithm based on keys between specific range. It works by counting the number of objects having distinct key values. Then doing some arithmetic to calculate the position of each object in the output sequence.
function CountingSort(&$data, $n, $min, $max) {
$cLen = $max - $min + 1;
$z = 0;
for ($i = 0; $i < $cLen; $i++)
$count[$i] = 0;
for ($i = 0; $i < $n; $i++)
$count[$data[$i] - $min]++;
for ($i = $min; $i <= $max; $i++)
{
while ($count[$i - $min]-- > 0)
{
$data[$z] = $i;
$z++;
}
}
$count = null;
}
Example
$data = array(-1, 25, -58964, 8547, -119, 0, 78596);
CountingSort($data, 7, -58964, 78596);
Output
-58964
-119
-1
0
25
8547
78596