-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_algos.php
70 lines (64 loc) · 1.37 KB
/
search_algos.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
function search($needle, $haystack)
{
$result=false;
foreach ($haystack as $key => $value)
if ($value==$needle)
{
$result=$key;
break;
}
return $result;
}
function binary_search($needle, $haystack, $min_key=0, $max_key=0, $depth=0)
{
if ($depth==0)
{
$max_key=count($haystack);
$min_key=0;
}
if ($max_key-$min_key>0) //when value can't be found, min will be equal to max and it will stop
{
$mid_key=$min_key+intdiv($max_key-$min_key, 2);
switch ($needle<=>$haystack[$mid_key])
{
case -1:
$result=binary_search($needle, $haystack, $min_key, $mid_key, $depth+1);
break;
case 0:
$result=$mid_key;
break;
case 1:
$result=binary_search($needle, $haystack, $mid_key, $max_key, $depth+1);
break;
}
}
else
$result=false;
return $result;
}
function randomise($size, $max=1000000000, $sequential=false)
{
$result=[];
$duplicate=[];
if ($sequential)
{
$jump=floor($max/$size);
$result[0]=rand(1, $jump);
for ($i=1; $i<$size; $i++)
{
$result[$i]=$result[$i-1]+rand(1, $jump);
}
}
else
{
for ($i=0; $i<$size; $i++)
{
do $value=rand(1, $max); while (isset($duplicate[$value]));
$duplicate[$value]=true;
$result[]=$value;
}
}
return $result;
}
?>