Home > Backend Development > PHP Tutorial > In-depth understanding of several PHP algorithms: PHP bubbling, PHP bisection, PHP finding prime numbers, PHP multiplication table_PHP tutorial

In-depth understanding of several PHP algorithms: PHP bubbling, PHP bisection, PHP finding prime numbers, PHP multiplication table_PHP tutorial

WBOY
Release: 2016-07-21 15:08:41
Original
751 people have browsed it

Compilation of several PHP algorithms involves the following examples.
PHP Bubble
PHP Dichotomy
PHP Finding Prime Numbers
PHP Multiplication Table

PHP bubbling method example

Copy code The code is as follows:

//PHP bubbling from small to large
function maopao(&$arr)
{
if(!empty($arr))
{
for($i=0; $i                                                                                                                                                                                                                                                                                    $temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
return $arr;
}
}




php binary search code example


Copy code

The code is as follows: //Binary searchfunction erfenfa($a,$arr)
{
print_r($arr);
if(!empty($a) && !empty($arr ))
{
$start = 0;
$end = count($arr)-1;
$i = 0;
while($start <= $end) {
                        $i ++;                                                            {
                                                                           $step]);
return $a;
}
if($a >$arr[$step])
{
$start = $step;
}

if($a < $arr[$step])
{
$end = $step;
}
}
}

}






php find prime numbers – calculate the prime numbers between a and b. Code example

Copy code
The code is as follows:


//php find prime numbers - Calculate the prime numbers between a and b.
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($flag)
$temp[$i] = $i;
}
}
}
return $temp;
}
}





PHP output multiplication table - recursive code example

Copy code
The code is as follows:

//PHP output multiplication table - recursivefunction 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;
digi($a,$step);
}
}





PHP output multiplication table - loop code example

Copy code
The code is as follows:

//PHP output multiplication table - loopfunction chengfa($a,$step){ if( !empty($a) && !empty($step) ) {
for($i=$a;$i<=$step;$i++)
{
for($j=1;$j<=$i;$j++)
{
                                                                                                                                                          less less have been more > }
}
}
}







http://www.bkjia.com/PHPjc/327418.html

www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327418.html

TechArticle

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template