PHP method to find whether a column of ordered arrays contains a value

Time:2021-7-26

Question: for a column of ordered arrays, how to judge whether a given value exists in the array.

Idea: the simplest way to judge whether it exists is to directly cycle the array and compare each value. However, for ordered arrays, this writing does not make good use of the feature of “order”.

All we use is “dichotomy search”,

//Ordered array is

$arr = array(2,5,66,87,954,1452,5865);

//Find value

$str = 1452;

//Let's define three parameters first

$front = 0;// A start value subscript

$end = count($arr) - 1;// An end value subscript

$mid = intval(($front + $end) / 2);// Intermediate subscript

1. For the first comparison, we directly judge whether the search value STR is equal to the intermediate value Mid. if it is equal to, we directly return true;

2. If the search value STR is greater than the intermediate value mid, it means that the search value STR may be on the right side of the intermediate value, that is, the start value front needs to be re assigned = intermediate value mid + 1, the end value end does not change, and the intermediate value mid is the new start value + end value in turn;

3. If the search value STR is less than the middle value mid, it means that the search value STR may be on the left of the middle value, that is, the start value does not change, the end value end needs to be re assigned = middle value – 1, and the middle value mid is the start value + the new end value in turn;

—–As above, the incoming start value, end value and intermediate value are compared. Once the start value is greater than the end value, it indicates that it is not found and ends the query. Otherwise, it returns found.

The specific codes are as follows:

$str = 89;// Find value

$arr = [1,55,66,89,420];// Ordered array

$ren = find($arr, $str);

echo '<pre>';

var_dump($ren);

function find($arr, $str){

 $front = 0;// Start subscript

 $end = count($arr) - 1;// End subscript

 While ($front < = $end) {// the end value is greater than the start value, otherwise exit

  $mid = intval(($front + $end) / 2);// Intermediate subscript

  if($str == $arr[$mid]){

   return $mid;// There is a subscript for the direct return value

  }

  if($str > $arr[$mid]){

   $front = $mid + 1;// in front

  }

  if($str < $arr[$mid]){

   $end = $mid - 1;// rearwards

  }

 }

 return false;

}

Return result: 89 is the subscript 3 of the fourth element value

int(3)

The above is the details of PHP searching whether a column of ordered arrays contains a certain value (binary search). If you have any supplement, you can contact the developeppaer editor.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]