Basic data structure and PHP built-in function implementation_PHP tutorial

WBOY
Release: 2016-07-21 14:57:30
Original
876 people have browsed it

Binary search function

//Binary search (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 search function

//Sequential search (search for 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 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;
}
Implementation of bubble 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;
}
Quick sort 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; $iif ($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;
}

//Intercept substring function to implement


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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364046.htmlTechArticleBinary search function //Binary search (search for an element in an array) function bin_sch($array, $low, $high, $k){ if ($low = $high){ $mid = intval(($low+$high)/2); if ($array[$mid] == $...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!