PHP simple array Lookup algorithm sharing

Source: Internet
Author: User
The search for an array in PHP can be looked up in order or by dichotomy. The order lookup is relatively simple, that is, to compare lookups. But the disadvantage is also more obvious, if the found element happens in the last, too many cycles.

1. Sequential Lookup Algorithm Description

Finds each element in an array, confirming that there is one, and returns the position information of the element when it exists. You can set the flag information with an initial value of false. Locate the direct output location and set the flag to true. The loop end flag is still false and is not found.

The code reflects:


$arr =[123,19,38,29,10,34];function Search ($arr, $target) {    //Parameters: Target array target element foreach ($arr as $key + = $value) {if ($ Value = = $target) {return $key. ' <br> ';}} return false;}

2. Two-point method to find the algorithm description

Suppose the array is in strict ascending order. If the target element is greater than the middle value, the lookup range is halved to the right. If the value of the target element is less than the value of the intermediate element, the lookup range is halved to the left.

The code reflects:

function Half_search ($arr, $target) {//defines the initial first, the subscript range of the last element $len = count ($arr  ); $left =0; $right = $len -1;//loop Find//range constantly moving, must satisfy a condition//the subscript of the leftmost element is less than or equal to the subscript of the right element while ($left <= $right) {//subscript of the intermediate element $middle = Floor (($left + $right)/2); The target element is compared to the intermediate element if ($target = = $arr [$middle]) {return $middle;}//If the target element is less than the middle element//range to the left by half if ($target < $arr [$middle] {$right = $middle-1;}//If the target element is greater than the middle element//range to the right by half if ($target > $arr [$middle]) {$left = $middle + 1;}} The loop terminated//did not find return false;} 
Related Article

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.