PHP third play-use the binary search method to find the element location in the array

Source: Internet
Author: User
Tags floor function
In php, we can use the array_search () function to find the key name of the element value in a number Group. similarly, we can use the binary method to find the key names of elements in the array. so what is a binary classification? I will explain: if the data is sorted in ascending order, we will start from the middle of the data to query... SyntaxHighlighter. all ();

In php, we can use the array_search () function to find the key name of the element value in a number Group.

Similarly, we can use the binary method to find the key names of elements in the array.
So what is a binary classification?

Let me explain:
If the data is sorted in ascending order, we start from the middle of the data. if the given value is exactly the same as the value at the current position, the search is successful, if the given value is smaller than the value at the current position, we will find the value of the first half of the value based on the current value. Otherwise, if the given value is greater than the current value, then, take the current value as the standard to find the value in the second half.

That is to say, the data searched by the binary method must be sorted.
Next we try to find the location of a value in array:
Function Search ($ a, $ val ){
$ Low = 0;
$ High = count ($ a)-1;
While ($ low <= $ high ){
$ Mid = intval ($ low + $ high)/2 );
If ($ a [$ mid] ==$ val)
Return $ mid;
If ($ a [$ mid]> $ val ){
$ High = $ mid-1;
} Else {
$ Low = $ mid + 1;
}
}
Return-1;
}
?>
In the preceding functions, the first parameter is the input array, and the second parameter is the value to be queried for this array.
We set a minimum value of $ low to 0 for the array key, and set a maximum value of $ high to minus one for the array length, then we can set the median value to intval ($ low + $ height)/2 ),
That is to say, when the length of an array element is an odd number, the median of the array is exactly in the middle,
For example, if the array length is 5, the center value is exactly 2, that is, (0 + 4)/2,
0, 1, 2, 3, 4
When the length of our array is an even number, the above formula cannot be used in addition, so we need to take an integer, whether it is a further method to take an integer or a rounding method to take an integer.
For example, if the length of the array is 6, the median value is (0 + 5)/2, and the value is 2.5, the key value of 2.5 in the array does not exist, so we need to take an integer of 2.
0, 1, 2, 3, 4, 5
If the integer intval or floor function is used to obtain 2 keys, if ceil is used to obtain an integer, the obtained key is 3.

Let's take the above array as an example:
If the length of the array is 10, the center value is intval (0 + (10-1)/2), which is 4, the index is 4, that is, the fifth element in the array "5"
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Okay, let's start to judge,
1. if the value to be queried happens to be "5", the search is successful and the index of this value is directly returned.
That is
If ($ a [$ mid] ==$ val)
Return $ mid;
2. if the value to be queried is less than "5", we need to set the value of $ high to the center value minus one, that is, 4-1, and returns the first half (red part) of the median in the query)
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
That is:
If ($ a [$ mid]> $ val ){
$ High = $ mid-1;
}

3. if the value to be queried is greater than "5", we need to set the value of $ low to the center value plus one, that is, 4 + 1, and return the second half (red part) of the intermediate value of the query)
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
That is:
Else {
$ Low = $ mid + 1;
}
After setting, the query continues. if the condition $ low <= $ high is met, the query is repeated until the condition is invalid, that is, when the queried range is smaller than an array element, indicates that the value to be queried does not exist in the array.-1 is returned.
While ($ low <= $ high ){
.......
}
Return-1;
The disadvantage of this function is:
If there are repeated values in the array, you can only find one of the key values. if you are interested in shoes, you can improve the above functions and reply with the Post.

 

From zdrjlamp

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.