PHP half-fold (Binary) Search Algorithm

Source: Internet
Author: User
The sorting and search algorithms are put together just now, which makes it inconvenient to read. Therefore, we will use the search algorithms for discussion. php implements binary search based on quick sorting.
/*** Description: PhP class that implements the binary search algorithm * @ author wzy */class binary_search {public $ arr; Public $ key; function _ construct ($ arr, $ key) {// The array initialized here is already an ordered array $ this-> arr = $ arr; $ this-> key = $ key;} function binarysearch () {$ start = 0; $ end = count ($ this-> ARR)-1; while ($ start <= $ end) {// The value of mid can be an upper or lower integer $ mid = Ceil ($ start + $ end)/2 ); // $ mid = ($ start + $ end)> 1; // $ mid = intval ($ start + $ end)/2 ); if ($ this-> arr [$ mid] <$ this-> key) {$ start = $ Mid + 1 ;} else if ($ this-> arr [$ mid]> $ this-> key) {$ end = $ mid-1 ;}else {return $ mid ;}}}}
You may also encounter this situation. The elements in the array have repeated data, and the position of the first element in the repeated data needs to be returned, for example, $ arr = array, 6, 6, 6, 7, 8); when searching for 6, the returned position should be 5, rather than other (the subscript starts counting from 0), so you need to judge in the returned mid, the Code is as follows:
/*** Description: PhP class that implements the binary search algorithm * @ author wzy */class binary_search {public $ arr; Public $ key; function _ construct ($ arr, $ key) {// The array initialized here is already an ordered array $ this-> arr = $ arr; $ this-> key = $ key;} function binarysearch () {$ start = 0; $ end = count ($ this-> ARR)-1; while ($ start <= $ end) {// The value of mid can be an upper or lower integer $ mid = Ceil ($ start + $ end)/2 ); // $ mid = ($ start + $ end)> 1; // $ mid = intval ($ start + $ end)/2 ); if ($ this-> arr [$ mid] <$ this-> key) {$ start = $ Mid + 1 ;} else if ($ this-> arr [$ mid]> $ this-> key) {$ end = $ mid-1 ;} else {// returns the first matched element for ($ I = $ mid-1; $ I >=0; $ I --) {if ($ this-> arr [$ I] ==$ this-> key) {$ mid = $ I;} else {break ;}} return $ mid ;}}}}

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.