Use PHP to implement binary search algorithm code sharing _ PHP Tutorial

Source: Internet
Author: User
Use PHP to share binary search algorithm code. Method 1: [binary search requirements]: 1. the sequential storage structure must be used. 2. the values must be sorted by the keyword size. [Advantages and disadvantages] the advantage of the half-lookup method is that the number of times is small. The first method is as follows:
[Binary search requirements]: 1. the sequential storage structure must be used. 2. the values must be sorted by the keyword size.
[Advantages and disadvantages] the advantage of the half-lookup method is that the query speed is fast and the average performance is good. The disadvantage is that the table to be queried is an ordered table and it is difficult to insert or delete the table. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.
[Algorithm idea] first, compare the keywords recorded in the middle of the table with the search keywords. if the two are the same, the search is successful; otherwise, the table is divided into the first and last sub-tables by using the intermediate position record. if the keyword recorded in the middle position is greater than the search keyword, the former sub-table is further searched. Otherwise, the latter sub-table is further searched.

The code is as follows:


// Author: distant expectations
// QQ: 15624575
// Home: http://www.phptogether.com/
// An array of forward sorting
$ Arr = array (1, 3, 5, 7, 9, 11 );
// Array of reverse sorting
$ Arr2 = array (11,9, 7,5, 3,1 );
// Perform binary search on the forward sorted array
Function searchpart ($ arr, $ x ){
$ Start = 0;
$ End = count ($ arr)-1;
While ($ start <= $ end ){
$ Mid = intval ($ start + $ end)/2); // you only need to make sure that the calculated value of the lower mark of the intermediate item is an integer. you can also rounding it out without affecting the result.
If ($ arr [$ mid]> $ x) {// if the value of the intermediate item is greater than the value to be queried, the difference value is on the left of the intermediate item. Therefore, the starting subscript remains unchanged, the subscript is changed to the subscript of the intermediate item minus 1. if $ arr [0]-$ arr [5] is searched for the first time, the next search will be performed.
$ End = $ mid-1; // $ arr [0]-$ arr [1]
} Elseif ($ arr [$ mid] <$ x) {// if the value of the intermediate item is smaller than the value to be queried, the difference value is on the right of the intermediate item. Therefore, the ending subscript remains unchanged, the initial subscript is changed to the intermediate subscript plus 1. if the first search is $ arr [0]-$ arr [5], the next search is, $ arr [3]-$ arr [5]
$ Start = $ mid + 1;
} Else {// found, returns the subscript of the value to be queried
Return $ mid;
}
}
}
// Perform binary search on the reverse sorted array
Function searchpart2 ($ arr, $ x ){
$ Start = 0;
$ End = count ($ arr)-1;
While ($ start <= $ end ){
$ Mid = intval ($ start + $ end)/2); // you only need to make sure that the calculated value of the lower mark of the intermediate item is an integer. you can also rounding it out without affecting the result.
If ($ arr [$ mid]> $ x) {// if the value of the intermediate item is greater than the value to be queried, the difference value is on the right of the intermediate item. Therefore, the ending subscript remains unchanged, the initial subscript is changed to the intermediate subscript plus 1. if $ arr [0]-$ arr [5] is searched for the first time, the next search will be performed.
$ Start = $ mid + 1; // $ arr [3]-$ arr [5]
} Elseif ($ arr [$ mid] <$ x) {// if the value of the intermediate item is smaller than the value to be queried, the difference value is on the left of the intermediate item. Therefore, the starting subscript remains unchanged, the ending subscript is changed to the intermediate subscript minus 1. if the first search is $ arr [0]-$ arr [5], the next search is, $ arr [0]-$ arr [1]
$ End = $ mid-1;
} Else {// found, returns the subscript of the value to be queried
Return $ mid;
}
}
}
Echo searchpart2 ($ arr, 5 ).'
';
Echo searchpart2 ($ arr2, 5 );
?>


PHP binary search algorithm implementation
Recently I sorted out my previous knowledge about algorithms. Although the algorithms used in WEB development are rare, I still back up some useful algorithms.
The half-lookup method is also called the binary lookup method. it fully utilizes the order relationship between elements and adopts the Grouping Policy. in the worst case, it can use O (log n) to complete the search task.
[Basic idea]
Divide n elements into two halves with roughly the same number. take a [n/2] For comparison with the x to be searched. if x = a [n/2], find x, algorithm termination. If xa [n/2], we only need to search for x in the right half of array.
Binary search is widely used and its ideas are easy to understand. The first binary search algorithm appeared as early as 1946, but the first fully correct binary search algorithm did not appear until 1962. In his book Writing Correct Programs, Bentley wrote that 90% of computer experts cannot write completely Correct binary search algorithms within two hours. The key to the problem is to accurately define the boundary of each search range and determine the termination conditions, and correctly summarize the various situations of parity, in fact, we can find that the specific algorithm is intuitive.
PHP binary search algorithm implementation

The code is as follows:


/**
* Binary search algorithm
*
* @ Param array $ arr ordered array
* @ Param int $ val: the value to be searched.
* @ Return int: the returned array subscript exists for the search value and-1 is not returned.
*/
Function binary_search ($ arr, $ val)
{
$ L = count ($ arr); // get the length of an ordered array
$ Low = 0;
$ High = $ l-1;
While ($ low <= $ high)
{
$ Middle = floor ($ low + $ high)/2 );
If ($ arr [$ middle] = $ val)
{
Return $ middle;
}
Elseif ($ arr [$ middle]> $ val)
{
$ High = $ middle-1;
}
Else
{
$ Low = $ middle + 1;
}
}
Return-1;
}
// Example
$ Arr = array (, 99 );
Echo binary_search ($ arr, 57 );

Partition [binary search requirements]: 1. the sequential storage structure must be used. 2. the values must be sorted by the keyword size. [Advantages and disadvantages] the advantage of the half-lookup method is that the number of times is small...

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.