It is not easy to write a correct and complete binary search.

Source: Internet
Author: User

Yesterday I went to youdao, and after a keyword search Error Correction design question, I came toProgramQuestion. Is the binary search of the loop array. At that time, the interview time was fixed between one hour and 15 minutes because of the long pull time. The interviewer said that I did not write it completely. Later, I wrote it back to the dormitory and found that it was still quite tricky. In particular, it is annoying to repeat things in the first place without thinking clearly. So write this article for warning.

Question: A is an ordered array, and B is an array that switches the forward and backward order after a is cut (that is, k elements are moved in the Right Loop ). Give an element E and ask how to efficiently find whether the E is in array B.

Idea: loop array + Binary Search

 

Solution Process:

1. Loop array, nothing more than real index and logic index, logic Index = (real index-K + Len) % Len.

2. How to correctly write a binary search

The following function is a common binary search process. However, if you use the logic index method directly, the following function may have a boundary problem. For example, r = 0, L = 0, then if the R-1, will get r = (R-1 + Len)/Len = len-1 suddenly jumped to the rightmost position, so l <R is still true, which will lead to an endless loop.

Bool normal_bs (int * a, int K, int S, int arrlen ){
Int L = 0, r = (arrlen-1), M;
While (l) <(R )){
M = (l) + (R)> 1;
If (A [m] = s ){
Return true;
} Else if (S> A [m]) {
L = (m + 1 );
} Else if (S <A [m]) {
R = m-1 );
}
}
Return false;
}

A better binary search program generally has a location exchange between l and R. Before a location exchange occurs, L = R. Therefore, you only need to do the following:

Bool normal_bs (int * a, int K, int S, int arrlen ){
Int L = 0, r = (arrlen-1), M;
While (true ){

If (L = r) {// cyclic Stop Condition

If (A [l] = s) return true;

Else return false;

}
M = (l) + (R)> 1;
If (A [m] = s ){
Return true;
} Else if (S> A [m]) {
L = (m + 1 );
} Else if (S <A [m]) {
R = m-1 );
}
}
Return false;
}

I personally think this is a better method than the previous one.

Here is an example of loop array + binary search. It's too weak to strengthen your efforts. Haha ~

// ================================================ ==============================================
// Name: binarysearch. cpp
// Author: jry
// Version:
// Copyright: Your copyright notice
// Description: Hello world in C ++, ANSI-style
// ================================================ ==============================================

# Include <iostream>
Using namespace STD;
Bool naive_debug = false;

Bool find (int * a, int K, int S, int arrlen ){

Int L = (0 + k) % arrlen, r = (arrlen-1 + k) % arrlen, M;
While (true ){
If (naive_debug) cout <A [l] <"," <A [R] <Endl;
If (L = r ){
If (A [l] = s) return true;
Else return false;
}

// The middle element.
M = (L-K + arrlen) % arrlen + (r-k + arrlen) % arrlen)> 1; // logic Index
M = (m + k) % arrlen; // Physical Index

If (A [m] = s ){
Return true;
} Else if (a [m] <s ){
L = (m + 1 + arrlen) % arrlen;
} Else if (a [m]> S ){
R = (m-1 + arrlen) % arrlen;
}
}
Return false;
}

Bool normal_bs (int * a, int K, int S, int arrlen ){
Int L = 0, r = (arrlen-1), M;
While (l) <(R )){
M = (l) + (R)> 1;
If (A [m] = s ){
Return true;
} Else if (S> A [m]) {
L = (m + 1 );
} Else if (S <A [m]) {
R = m-1 );
}
}
Return false;
}

Int main (){

Int A [] = {4, 5, 6, 7, 8, 1, 2, 3, 4}, K = 5;
Int B [] = {6, 0, 6,-11110, 3, 1, 10}, Len = sizeof (a)/sizeof (INT );
For (INT I = 0; I <(INT) (sizeof (B)/sizeof (INT); I ++ ){
If (find (A, K, B [I], Len ))
Cout <B [I] <"in array." <Endl;
Else
Cout <B [I] <"not in array." <Endl;
}
Cout <"!!! Done !!! "<Endl; // prints !!! Hello world !!!
Return 0;
}

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.