forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counting_Sort.php
39 lines (32 loc) · 846 Bytes
/
Counting_Sort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
$list = array(3, 4, 5, 4, 7);
//Counting sort Function
function countingsort($list , $largest, $arrsize )
{
$size = $largest + 1;
$b= new SplFixedArray(10000);
for($i=0 ; $i<$arrsize;$i++)
{
$val = $list[$i];
$b[$val] = $b[$val]+1;
}
$l = 0;
$r=0;
$c= array(0,0,0,0,0);
for ($j = 0; $l < $size; $j++)
{
while ($b[$l] != 0)
{
$c[$r] = $l;
$r++;
$b[$l]=$b[$l]-1;
}
$l++;
}
for ( $k=0 ; $k<$arrsize;$k++)
echo $c[$k];
}
//Calling Countingsort function
countingsort($list , 7 , 5);
// output 34457
?>