Basic data structure and php built-in function implementation _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Basic data structure and php built-in function implementation. Binary lookup function binary lookup (find an element in an array) functionbin_sch ($ array, $ low, $ high, $ k) {if ($ low $ high) {$ midintval ($ low + $ high) 2); if ($ array [$ mid] $ binary lookup function

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

Implementation of sequential search functions

// 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;
}
}
Implementation of linear table deletion functions

// 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;
}
Implementation of the bubble sorting function:

// 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;
}
Quick sorting function implementation

// Fast sorting (array sorting)
Function quicksort ($ 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 = quicksort ($ left_arr );
$ Right_arr = quicksort ($ right_arr );

Return array_merge ($ left_arr, array ($ key), $ right_arr );
}

PHP built-in string function implementation

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

// Implement the substring function


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 ++ ){

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next page

Else // 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] = $...

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.