In-depth understanding of several PHP Algorithms: PHP bubble, PHP binary, PHP Prime, PHP multiplication table

Source: Internet
Author: User
This article provides a detailed analysis of PHP bubble, PHP binary, PHP prime number, and PHP multiplication table. For more information, see Several PHP algorithms involve the following examples.
PHP bubble
PHP binary
Evaluate the prime number in PHP
PHP multiplication table

PHP bubble method example
The code is as follows:
// PHP bubbles from small to large
Function maopao (& $ arr)
{
If (! Empty ($ arr ))
{
For ($ I = 0; $ I {
If ($ arr [$ I]> $ arr [$ j])
{
// Start exchange
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ j];
$ Arr [$ j] = $ temp;
}
}
}
Return $ arr;
}
}

Php binary search code example
The code is as follows:
// Binary search
Function erfenfa ($ a, $ arr)
{
Print_r ($ arr );
If (! Empty ($ )&&! 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 $;
}
If ($ a> $ arr [$ step])
{
$ Start = $ step;
}

If ($ a <$ arr [$ step])
{
$ End = $ step;
}
}
}
}

Php calculates the prime number-calculates the prime number between a and B. Sample code
The code is as follows:
// Php calculates the prime number-calculates the prime number between a and B.
Function sushu ($ a, $ B)
{
If (! Empty ($ )&&! 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
The code is as follows:
// PHP output multiplication table-recursion
Function digui ($ a, $ step)
{
If ($ a> $ step) return;
If (! Empty ($ )&&! 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-example of loop code
The code is as follows:
// PHP output multiplication table-loop
Function chengfa ($ a, $ step)
{
If (! Empty ($ )&&! 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.