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

Source: Internet
Author: User
In-depth understanding of several PHP Algorithms: PHP bubble, PHP binary, PHP Prime, and PHP multiplication table. Several PHP algorithms involve the following examples. PHP bubble PHP binary method PHP evaluate Prime number PHP multiplication table PHP bubble method example copy code: PHP bubble from small to large functi 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'
';
}
}
}
}


Pipeline involves the following examples. PHP bubble PHP binary method PHP calculates prime number PHP multiplication table PHP bubble method example code: // PHP bubble from small to large functi...

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.