Basic data structure algorithm .? -------------------- Basic data structure algorithm ------------------ binary search (find an element in the array) functionbin_sch ($ array, $ low, $ high, $ k) {if ($ low $ h //--------------------
// Basic data structure algorithm
//--------------------
// Binary search (find an element in the 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 search (find an element in the 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;
}
}
// Delete a linear table (implemented in an array)
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 sorting)
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;
}
// Fast sorting (array sorting)
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;
}
// Truncate the substring
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 flip
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 ($ 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;
}
// Search for strings
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 );
$ 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-implemented string processing functions
//--------------------
// 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 the string
Function strcpy ($ s1, $ s2)
{
If (strlen ($ s1) = NULL |! Isset ($ s2) return;
For ($ I = 0; $ I $ S2 [] = $ s1 [$ I];
}
Return $ s2;
}
// Connection string
Function strcat ($ s1, $ s2)
{
If (! Isset ($ s1) |! Isset ($ s2) return;
$ Newstr = $ s1;
For ($ I = 0; $ I $ Newstr. = $ st [$ 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> 31 & $ c <107) $ c + = 20;
If ($ c> 106 & $ c <127) $ c-= 75;
$ 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 encryption function (corresponding to the php_decrypt function)
Function php_encrypt ($ str)
{
$ Encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
$ Decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468038559 ';
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 (corresponding to the php_encrypt function)
Function php_decrypt ($ str)
{
$ Encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
$ Decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468038559 ';
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;
}
?>
Http://www.bkjia.com/PHPjc/632482.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632482.htmlTechArticle? // -------------------- // Basic data structure Algorithm // ---------------------- // binary search (find an element in the array) function bin_sch ($ array, $ low, $ high, $ k) {if ($ low = $ h...