Binary search routines close to left and right

Source: Internet
Author: User

Close to left: only when Arr[mid]<target is right, nether Lo Propulsion, mid=lo+ ((Hi-lo) >>1), last position in Lo


Close to right: only when arr[mid]>target to the left, upper bound hi Propulsion, mid=lo+ ((hi-lo+1) >>1), last position in hi


public class test649 {public
	static void Main (string[] args) {
		int[] arr={ 1,1,1,1,2,2,3,4,5,6,6,6,7,7,7,7,7,8,9,10,10,17,18,20};
		System.out.println (Binaryleftsearch (arr, 7));
		System.out.println (Binaryrightsearch (arr, 7));
		System.out.println ();
	}
	
	
	
	public static int Binaryleftsearch (int[] arr,int target) {
		int len=arr.length;
		int lo=0,hi=len-1;
		while (Lo


Output

12

16

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.