//--------------------
//Basic data structure algorithm
//--------------------
//Two points lookup (find an element in an array)
function Bin_sch ($array, $low, $high, $k) {
if ($low <= $high) {
$mid = Intval (($low + $high)/2);
if ($array [$mid] = = $k) {
return $mid;
}elseif ($k < $array [$mid]) {
return Bin_sch ($array, $low, $mid-1, $k);
}else{
return Bin_sch ($array, $mid + 1, $high, $k);
}
}
return-1;
}
//Sequential lookup (find an element in an array)
function Seq_sch ($array, $n, $k) {
$array [$n] = $k;
For ($i =0; $i < $n; $i + +) {
if ($array [$i]== $k) {
Break ;
}
}
if ($i < $n) {
return $i;
}else{
return-1;
}
}
///Linear table deletions (implemented in arrays)
function Delete_array_element ($array, $i)
{
$len = count ($array);
For ($j = $i; $j < $len; $j + +) {
$array [$j] = $array [$j +1];
}
Array_pop ($array);
return $array;
}
//bubble sort (array sort)
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;
}
//Quick sort (array sort)
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;
$count = 0;
While (1) {
if ($str [$count]! = NULL) {
$count + +;
continue;
}else{
Break ;
}
}
return $count;
}
//intercept sub-strings
function substr ($str, $start, $length =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 + +) {
$substr. = $str [$i];
}
}
if ($length > 0) {
For ($i = $start; $i < ($start + $length); $i + +) {
$substr. = $str [$i];
}
}
if ($length < 0) {
For ($i = $start, $i < (strlen ($STR) + $length), $i + +) {
$substr. = $str [$i];
}
}
return $substr;
}
//String rollover
function Strrev ($STR)
{
if ($str = = ") return 0;
For ($i = (strlen ($str)-1), $i >=0; $i-) {
$rev _str. = $str [$i];
}
return $rev _str;
}
//string comparison
function strcmp ($s 1, $s 2)
{
if (strlen ($s 1) < strlen ($s 2)) return-1;
if (strlen ($s 1) > strlen ($s 2)) return 1;
for ($i =0; $i
if ($s 1[$i] = = $s 2[$i]) {
continue;
}else{
return false;
}
}
return 0;
}
//Find string
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 substitution
function Str_replace ($substr, $newsubstr, $str)
{
$m = strlen ($STR);
$n = strlen ($SUBSTR);
$x = strlen ($NEWSUBSTR);
if (STRCHR ($str, $substr) = = false) return false;
For ($i =0; $i <= ($m-$n + 1); $i + +) {
$i = STRCHR ($str, $substr);
$str = Str_delete ($str, $i, $n);
$str = Str_insert ($str, $i, $newstr);
}
return $str;
}
//--------------------
//self-implementing string processing function
//--------------------
//Insert a string
function Str_insert ($str, $i, $substr)
{
For ($j =0; $j < $i; $j + +) {
$startstr. = $str [$j];
}
for ($j = $i; $j
$laststr. = $str [$j];
}
$str = ($startstr. $substr. $laststr);
return $str;
}
//Delete a string
function Str_delete ($str, $i, $j)
{
For ($c =0; $c < $i; $c + +) {
$startstr. = $str [$c];
}
for ($c = ($i + $j); $c
$laststr. = $str [$c];
}
$str = ($startstr. $laststr);
return $str;
}
//Copy string
function strcpy ($s 1, $s 2)
{
if (strlen ($s 1) ==null | |!isset ($s 2)) return;
for ($i =0; $i
$s 2[] = $s 1 [$i];
}
return $s 2;
}
//Connection string
function strcat ($s 1, $s 2)
{
if (!isset ($s 1) | |!isset ($s 2)) return;
$newstr = $s 1;
for ($i =0; $i
$newstr. = $st [$i];
}
return $newsstr;
}
//Simple coding function (corresponding to the Php_decode function)
function Php_encode ($STR)
{
if ($str = = "&& strlen ($STR) >128) return false;
for ($i =0; $i
$c = Ord ($str [$i]);
if ($c >31 && $c <107) $c + +;
if ($c >106 && $c <127) $c-=;
$word = Chr ($c);
$s. = $word;
}
return $s;
}
//Simple decoding function (corresponding to the Php_encode function)
function Php_decode ($STR)
{
if ($str = = "&& strlen ($STR) >128) return false;
for ($i =0; $i
$c = ord ($word);
if ($c >106 && $c <127) $c = $c -20;
if ($c >31 && $c < 107) $c = $c +75;
$word = Chr ($c);
$s. = $word;
}
return $s;
}
//Simple cryptographic function (corresponding to the Php_decrypt function)
function Php_encrypt ($STR)
{
$encrypt _key = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
$decrypt _key = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';
if (strlen ($str) = = 0) return false;
for ($i =0; $i
for ($j =0; $j
if ($str [$i] = = $encrypt _key [$j]) {
$enstr. = $decrypt _key[$j];
Break ;
}
}
}
return $enstr;
}
//Simple decryption function (corresponds to the Php_encrypt function)
function Php_decrypt ($STR)
{
$encrypt _key = ' abcdefghijklmnopqrstuvwxyz1234567890 ';
$decrypt _key = ' ngzqtcobmuhelkpdawxfyivrsj2468021359 ';
if (strlen ($str) = = 0) return false;
for ($i =0; $i
for ($j =0; $j
if ($str [$i] = = $decrypt _key [$j]) {
$enstr. = $encrypt _key[$j];
break;
}
}
}
return $enstr; /span>
}
?>
original address: http://www.360doc.com/ content/14/0319/16/15326015_361910933.shtml
Above describes the PHP implementation of a variety of classic algorithms, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.