A deep understanding of PHP several algorithms: PHP bubble, php binary, PHP to calculate prime, php multiplication table _php Skills

Source: Internet
Author: User

Several examples of how PHP algorithms are collated involve the following.
PHP bubbling
PHP Two-way
PHP to calculate prime
PHP multiplication Table

Example of a php bubbling method

Copy Code code as follows:

PHP bubbling from small to large
Function Maopao (& $arr)
{
if (!empty ($arr))
{
for ($i =0; $i <count ($arr); $i + +)
{
if ($arr [$i]> $arr [$j])
{
Start swapping
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}
}
return $arr;
}
}

PHP Binary Lookup code example

Copy Code code as follows:

Two-Way Search
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;
}
}
}
}

PHP Calculate Prime-Calculates the prime number between A and B. code example

Copy Code code as follows:

PHP Calculate Prime-Calculates the prime number 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 ($i% $x ==0)
{
$flag = false;
Break
}
}
if ($flag)
{
$temp [$i] = $i;
}
}
}
return $temp;
}
}

PHP Output Multiplication Table-Recursive code example

Copy Code code as follows:

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-Circular code example

Copy Code code as follows:

PHP Output Multiplication Table-loop
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 '
‘;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.