Files
TheAlgorithms-PHP/Sorting/CountSort.php
Anamarija Papić b3774d70b0 fix count sort.
- no min and max as function parameters
- count sort tests are now passing
2023-10-06 19:57:48 +02:00

31 lines
445 B
PHP

<?php
/**
* @param $array
* @return mixed
*/
function countSort($array)
{
$count = array();
$min = min($array);
$max = max($array);
for ($i = $min; $i <= $max; $i++) {
$count[$i] = 0;
}
foreach ($array as $number) {
$count[$number]++;
}
$z = 0;
for ($i = $min; $i <= $max; $i++) {
while ($count[$i]-- > 0) {
$array[$z++] = $i;
}
}
return $array;
}