Analysis of Implementation Method of PHP Block Query

  • 2021-09-24 21:52:09
  • OfStack

This paper describes the implementation method of PHP block query with examples. Share it for your reference, as follows:

Block query is a query method between sequential query and halved query.

In fact, a half-fold query is a half-fold block query every time, so a block query is a query method that divides an array into blocks and then queries each block.

The array in this example has been sorted, and it is queried sequentially after being divided into blocks.

php code:


<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
print_r(blockSearch(3,1,$arr));
function blockSearch($block,$key,$arr){
  $length = count($arr);
  $position = 0;
  while($length >= $position){// When the array elements are compared, the loop ends 
    for($i=1;$i<=$block;$i++){// The number of cycles is the size of the defined block 
      if($arr[$position] == $key){// Element found 
        return 'value:'.$arr[$position] .';position:'.$position;
      }
      $position++;// Per comparison 1 Second, the position moves backward 1 Times 
    }
  }
}
?>

Run results:

value:1;position:0

More readers interested in PHP can check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: