PHP-based Binary Search Algorithm instance analysis and algorithm instance analysis

Source: Internet
Author: User

PHP-based Binary Search Algorithm instance analysis and algorithm instance analysis

This example describes the binary search algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:

The binary search method requires an ordered array.

Assume that our array is an incremental array. First, we need to find the center position of the array.

I. To know the intermediate position, you need to know the start position and end position, and then take out the value of the intermediate position to compare it with our value.
II. If the median value is greater than our given value, it indicates that our value must be second before the intermediate position. Because before the intermediate value, we need to change the value to the end position, the value of the end position should be the center position at this time.
3. Otherwise, if the median value is smaller than the given value, it indicates that the given value is in the middle position. At this time, we need to divide the value of the next part again, because after the median value, therefore, we need to change the value of the starting position. At this time, the value of the starting position should be our intermediate position until we find the specified value.
4. Or the center value is equal to the initial start position or end position (indicating that the given value is not found). Here we use the code to implement it ~

// Cyclically implement function getValue ($ num, $ arr) {// find the center position of the array $ length = count ($ arr); $ start = 0; $ end = $ length; $ middle = floor ($ start + $ end)/2); // cyclically judges while ($ start> $ end-1) {if ($ arr [middle] ==$ num) {return middle + 1;} elseif ($ arr [middle] <$ num) {// if the value to be searched is more than the median value of the current array, it means that the value is in the second half of the array. // The starting position is changed to the current value of the middle, the end position remains unchanged. $ Start = $ middle; $ middle = floor ($ start + $ end)/2);} else {// otherwise $ end = $ middle; $ middle = floor ($ start + $ end)/2);} return false ;}

// Cyclically implement function getValue ($ num, $ arr) {// find the center position of the array $ length = count ($ arr); $ start = 0; $ end = $ length; $ middle = floor ($ start + $ end)/2); // cyclically judges while ($ start> $ end-1) {if ($ arr [middle] ==$ num) {return middle + 1;} elseif ($ arr [middle] <$ num) {// if the value to be searched is more than the median value of the current array, it means that the value is in the second half of the array. // The starting position is changed to the current value of the middle, the end position remains unchanged. $ Start = $ middle; $ middle = floor ($ start + $ end)/2);} else {// otherwise $ end = $ middle; $ middle = floor ($ start + $ end)/2);} return false ;}

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.