TheAlgorithms-PHP/sorting/countSort.php

30 lines
442 B
PHP
Raw Normal View History

2020-11-01 15:31:06 +01:00
<?php
/**
* @param $array
* @param $min
* @param $max
* @return mixed
*/
function countSort($array, $min, $max)
{
$count = 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;
}