Deeply understand several PHP algorithms :PHP bubble PHP dichotomy PHP prime PHP multiplication table
- 2020-06-12 08:34:37
- OfStack
PHP several algorithm collation involves the following examples.
PHP bubbling
PHP2 points method
Prime PHP o
PHP multiplication table
Example of PHP bubble method
//PHP The bubbling Since the childhood
function maopao(&$arr)
{
if(!empty($arr))
{
for($i=0;$i<count($arr);$i++)
{
if($arr[$i]>$arr[$j])
{
// Began to exchange
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
return $arr;
}
}
php2 points to find code examples
//2 Points method lookup
function erfenfa($a,$arr)
{
print_r($arr);
if(!empty($a) && !empty($arr))
{
$start = 0;
$end = count($arr)-1;
$i = 0;
while($start <= $end) {
$i ++;
$step = floor($end / 2);
if($a == $arr[$step])
{
print_r($arr[$step]);
return $a;
}
if($a >$arr[$step])
{
$start = $step;
}
if($a < $arr[$step])
{
$end = $step;
}
}
}
}
The primes at php count between a and b. Code sample
//php For prime - To calculate a to b Between the prime Numbers.
function sushu($a,$b)
{
if(!empty($a) && !empty($b))
{
if($b<$a) return;
$temp = array();
for($i=$a;$i <=$b;$i++)
{
$j = intval(sqrt($i));
$flag = true;
if($i<=3)
{
$temp[$i] = $i;
}else
{
for($x=2;$x<=$j;$x++)
{
if($i%$x==0)
{
$flag = false;
break;
}
}
if($flag)
{
$temp[$i] = $i;
}
}
}
return $temp;
}
}
PHP output times table - recursive code example
//PHP Output multiplication table - recursive
function digui($a,$step)
{
if($a >$step) return;
if( !empty($a) && !empty($step) )
{
for($i=1;$i<=$a;$i++)
{
echo $i.'*'.$a.'='.$a*$i. " \t " ;
if($i == $a ) echo '
' ;
}
$a = $a + 1;
digui($a,$step);
}
}
PHP output multiplication table - loop code example
//PHP Output multiplication table - cycle
function chengfa($a,$step)
{
if( !empty($a) && !empty($step) )
{
for($i=$a;$i<=$step;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j.'*'.$i.'='.$i*$j. " \t " ;
if($i==$j) echo '
' ;
}
}
}
}