Data Structure and string processing code sorting

Source: Internet
Author: User
Sort out some of the previously written algorithms and string processing functions. This is just for learning.

Author: heiyeluren
Blog: http://blog.csdn.net/heiyeshuwu
Date:

<?
//--------------------
// 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 <count ($ array); $ 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 <strlen ($ S1); $ 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 <strlen ($ Str); $ 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 <strlen ($ Str); $ 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 <strlen ($ S1); $ I ++ ){
$ S2 [] = $ S1 [$ I];
}
Return $ S2;
}

// Connection string
Function strcat ($ S1, $ S2)
{
If (! Isset ($ S1) |! Isset ($ S2) return;
$ Newstr = $ S1;
For ($ I = 0; $ I <count ($ S); $ 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 <strlen ($ Str); $ 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 <strlen ($ Str); $ 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 <strlen ($ Str); $ I ++ ){
For ($ J = 0; $ j <strlen ($ encrypt_key); $ 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 <strlen ($ Str); $ I ++ ){
For ($ J = 0; $ j <strlen ($ decrypt_key); $ J ++ ){
If ($ STR [$ I] ==$ decrypt_key [$ J]) {
$ Enstr. = $ encrypt_key [$ J];
Break;
}
}
}

Return $ enstr;
}

?>
 

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.