Interview Review = Hua Shan tianjian (1 )? Binary Search is also a disease: BSearch

Source: Internet
Author: User

 

I saw an interesting article and solved the problem... I think it's a bit interesting ~ The peak of graduation VS job interview.

 

Two sentences in programming Pearl River:

  1. Despite the ample time, only about 10% of professional programmers can write the correct binary search.
  2. Although the first binary lookup program was announced in 1946, the first bug-free binary lookup program appeared in 1962.

When I saw this, I felt a little exaggerated. We will not discuss whether it takes 20 years for people to write the correct code, but these two statements should at least tell us not to look down on binary search.

Indeed, people who have written about bsearch know that it is difficult to figure out the exit of iteration in a short time (I am often confused ). Of course, the general binary search can be basically written now (after so many times, the back is also memorized), but it does not mean that the binary search is actually understood. For example, we can change the prototype to find the number that is greater than x and closest to x from an ordered array.

To put it bluntly, it is binary search, but what is the exit of iteration?

 

I. Prototype JAVA code representation of binary search algorithms

// Traditional binary search PS: The values in array a are arranged in ascending order. If the target X is found, the base value is returned. If the target X is not found, the system returns-1 public static int bsearch (int [], int x) {int l = 0, h =. length-1; int m; while (h> = l) {m = (l + h)/2; if (a [m] = x) {return m ;} else if (a [m] <x) {l = m + 1;} else {h = m-1;} System. out. println ("l:" + l + "h:" + h); return-1 ;}

 

Consider the case where the element to be searched is not in the sequence: The function will return-1. What do l and h point?

It can be proved that l points to the first element greater than x, and r points to the first element less than x. (Why? The last step of iteration must be that l and h point to the same element. Why is it bigger than h? Think about it)

 

2. deformation 1

 

// Function Description: Search for the number that is bigger than X but closest to X in an ordered array. // Binary Search PS: The values in array a are arranged from small to large, if the target X is found, the base value is returned. If not, the-1 public static int bsearch_more (INT [] A, int X) {int L = 0, H =. length-1; int m; while (h> = L) {M = (L + H)/2; if (a [m] = X) {return (m + 1)>. length-1? -1: m + 1;} else if (a [m] <X) {L = m + 1;} else {H = s-1;} system. out. println ("L:" + L + "H:" + H); return l ;}

Iii. deformation 2

// Function Description: search for the number smaller than X but closest to X in an ordered array. // Binary Search PS: The values in array a are arranged from small to large, if the target X is found, the base value is returned. If not, the-1 public static int bsearch_less (INT [] A, int X) {int L = 0, H =. length-1; int m; while (h> = L) {M = (L + H)/2; if (a [m] = x) {return m-1) <0? -1 M-1;} else if (a [m] <X) {L = m + 1;} else {H = s-1;} system. out. println ("L:" + L + "H:" + H); Return h ;}

Appendix: Main Function Test code Java Representation

Public static void main (string [] ARGs) {// todo auto-generated method stub // traditional binary search int [] A = {, 20 }; int Sign = bsearch (A, 11); system. out. println (sign); Sign = bsearch_more (A, 18); system. out. println (A [sign]); Sign = bsearch_less (A, 5); system. out. println (A [sign]);}

 

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.