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'
';
}
}
}
}