Home  >  Article  >  Backend Development  >  PHP implements various classic algorithms

PHP implements various classic algorithms

WBOY
WBOYOriginal
2016-07-29 09:01:03896browse

//---------- -------------
//Binary search (search for an element in an array)
function bin_sch($array, $low, $high, $k){
if ( $low <= $high){
return $ MID; d} Elseif ($ k & lt; $ array [$ mid]) {
Return bin_sch ($ array, $ low, $ k);
} else {
{ B Return bin_sch ($ Array, $ MID+1, $ High, $ K);
}}}
Return -1; Element)
function seq_sch($array, $n, $k){
$array[$n] = $k;
for($i=0; $i<$n; $i++) {                                                                                                 
if ($i<$n){
return $i;
}else{
return -1;
}
}
//Deletion of linear table (implemented in array)
function delete_array_element($array, $i)
{
                                                                                                          use   use use use use use use through use through use through through through through out out out out out out off out out out out out out out out out out out out of the into                                                                                                                                     = $array [$ j+1];
                                                                                                                                                                                   
//冒泡排序(数组排序)  
function bubble_sort( $array)  
{  
        $count = count( $array);  
        if ($count <= 0 ) return false;  
        for($i=0 ; $i<$count; $i ++){  
                for($j=$count-1 ; $j>$i; $j--){  
                        if ($array[$j] < $array [$j-1]){  
                                 $tmp = $array[$j];  
                                 $array[$j] = $array[ $j-1];  
                                $array [$j-1] = $tmp;  
                        }  
                }  
        }  
        return $array;  
}  
//快速排序(数组排序)  
function quick_sort($array ) {  
        if (count($array) <= 1) return  $array;  
        $key = $array [0];  
        $left_arr  = array();  
        $right_arr = array();  
        for ($i= 1; $i
                if ($array[ $i] <= $key)  
                        $left_arr [] = $array[$i];  
                else  
                         $right_arr[] = $array[$i ];  
        }  
        $left_arr = quick_sort($left_arr );  
        $right_arr = quick_sort( $right_arr);  
        return array_merge($left_arr , array($key), $right_arr);  
}
//-----------------------------
// PHP built-in string function implementation
//----- -------------------
//String length
function strlen ($str)
{
if ($str == ' ') return 0;

                                                                                                                                                                                                                     } Else {
Break;
}}
Return $ Count; =NULL)
{
if ($str== '' || $start>strlen($str )) return;
if (($length!=NULL) && ( $start>0) && ($length> strlen($str)-$start)) return;
if (( $length!=NULL) && ($start< 0) && ($length>strlen($str )+$start) ) return;
if ($length == NULL) $length = (strlen($str) - $start);
if ($start < 0){
for ($i =(strlen( $str)+$start); $i<(strlen ($str)+$start+$length ); $i++) {
                                                                 }
}
if ($length > 0){
for ($i= $start; $i<($start+$length); $i++) {
                                                                                   [$i];
                                      

        if ( $length < 0){  
                for ($i =$start; $i<(strlen( $str)+$length); $i++) {  
                        $substr .= $str[$i ];  
                }  
        }  
        return $substr;  
}  
//字符串翻转  
function strrev($str)  
{  
        if ($str == '') return 0 ;  
        for ($i=(strlen($str)- 1); $i>=0; $i --){  
                $rev_str .= $str[$i ];  
        }  
        return $rev_str;  
}  
//字符串比较  
function strcmp($s1,  $s2)  
{  
        if (strlen($s1) <  strlen($s2)) return -1 ;  
        if (strlen($s1) > strlen( $s2)) return 1;  
        for ($i =0; $i
                if ($s1[ $i] == $s2[$i]){  
                        continue;  
                }else{  
                        return false;  
                }  
        }  
        return  0;  
}  
//查找字符串  
function  strstr($str, $substr)  
{  
         $m = strlen($str);  
        $n = strlen($substr );  
        if ($m < $n) return false ;  
        for ($i=0; $i <=($m-$n+1); $i ++){  
                $sub = substr( $str, $i, $n);  
                if ( strcmp($sub, $substr) ==  0)  return $i;  
        }  
        return false ;  
}
//String replacement
function str_replace($substr, $newsubstr, $str)
{
$m = strlen($str);
$n = strlen($substr) ; i<=($m - $n+1); $i++){ ​​​
                                                                                        str_insert($str, $i, $newstr);
                                                                                                                   --
//Self-implemented string processing function
//-----------------------------
//Insert a string
function str_insert($str, $i, $substr)
{
for($j=0; $j<$i; $j++){
];
}                                                                                                                                                                       function str_delete($str, $i, $j)
{
for ( $c=0; $c<$i; $c++){

                                                                                                    $c=( $i+$j); $c

                                                 $str = ($startstr . $laststr );
return $str;
}
//Copy string
function strcpy($s1, $s2)
{
                                                                                                                                                         ==NULL || !isset( $s2)) return;
for ($i=0; $i

$s2[] = $s1 [$i ];
                                               
//Connection string
function strcat($s1, $s2)
{
if (!isset($s1) || !isset($s2)) return;
        $ newstr = $s1;
for($i=0; $i
} Return $ Newsstr;
}
// Simple encoding function (corresponding to PHP_DECODE function)
Function php_encode ($ Str) {
if ($ Str == '&& Strlen ( $str)>128) return false;
for( $i=0; $i $c = ord($str[$i]); If ($ C & GT; 31 && $ C & LT; 107) $ C+= 20;
IF ($ C & GT; 106 & $ C & LT; 127) $ C-= 75;
$ word = chr($c);
//Simple decoding function (corresponding to php_encode function)
function php_decode ($str)
{
if ($str==''&& strlen($str)>128) return false;
for($i=0; $i
                                                                                           $c< 107) $c = $c+75 ;
                                                                                                                                .
//Simple encryption function (corresponding to the php_decrypt function)
function php_encrypt($str)
{
$encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567 890'; if ( strlen($str) == 0) return false;
for ($i=0; $i
for ($j=0; $ j
                                                                               $enstr .=                                                                                      
                                                                                                         
}
} return $enstr;
}
//Simple decryption function (corresponding to the php_encrypt function)
function php_decrypt($str )
{
yivrsj2468021359';
if (strlen($str) == 0) return false;
for ($i= 0; $i
                                                                 if ($str [$i] == $decrypt_key [$j]){
                                               break;
                           return $ enstr;
}
?>
The above introduces the implementation of various classic algorithms in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:
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