Basic data structure and PHP built-in function implementation _php Tutorial

Source: Internet
Author: User
Two-part lookup function

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

Implementation of Sequential Lookup function

Order 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;
}
}
Implementation of the deletion function of linear table

Deletion of linear tables (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;
}
The implementation of the bubbling sort function:

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

Quick sort (Array sort)
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;
}

Implementation of truncated 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 + +) {

    • Total 3 Pages:
    • Previous page
    • 1
    • 2
    • 3
    • Next page

http://www.bkjia.com/PHPjc/364046.html www.bkjia.com true http://www.bkjia.com/PHPjc/364046.html techarticle binary Lookup Function//two-point 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] = = $ ...

  • 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.