mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 23:28:14 +01:00
30 lines
442 B
PHP
30 lines
442 B
PHP
|
<?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;
|
||
|
}
|