A binary lookup algorithm for finding duplicate elements

Source: Internet
Author: User

A binary lookup algorithm for finding duplicate elements

Binary search Algorithm idea: Also known as binary lookup, two-point lookup is suitable for a sorted set of data to find. Suppose you have an ascending data set that finds the most intermediate element in the ascending collection, divides the data collection into two subsets, compares the most intermediate element with the keyword key, returns if it is equal to key, or, if it is greater than the keyword, looks in the previous data collection; Until it is found, return-1 if not found.

Ideas:

1, first define two subscript, left = 0, right = arr.length-1;

2, because we do not know how many times to cycle, define a while loop, the termination condition is Right>left

3, because is a binary lookup, define a mid = left + (right-left)/2; To prevent excessive data overflow

4, define three if statements, if target = = = Arr[mid], return mid; This is the classic binary lookup, and we need to do this in the improvement

4.1, improve the classical binary algorithm, binary search is based on an ordered array, repeating the elements are together. We only need to modify the IF (target = = Arr[mid]), we need to return the first subscript that appears target, because we do not know that there are several duplicate elements in front of mid so we need a while (mid>=0) loop, mid--, Then compare to Arr[mid] and target, as long as it is not the same, terminate, return

5. If target < Arr[mid], right = mid-1;

6. If target > Arr[mid], left = mid + 1;

Know the idea, let's do the programming to realize it

/** * Find a binary search algorithm for repeating elements * ideas: * 1, first define two subscript, left = 0, right = arr.length-1; * 2, because we do not know how many times to cycle, define a while loop, the termination condition is right>left * 3, because it is a binary lookup, define a mid = left + (right-left)/2; Prevent excessive data overflow * 4, Define three if statements, if target = = = Arr[mid], return mid; This is the classic binary lookup, we need to do this in the improvement * 4.1, improve the classical binary algorithm, because there may be duplicate elements, we need to return the first to appear target subscript; because we don't know. There are several repeating elements in front of the mid * so we need a while (mid>=0) loop, mid--, then Arr[mid] and target, as long as it's not the same as terminating, returning * 5, if Target < Arr[mid], R     ight = mid-1;     * 6, if target > Arr[mid], left = mid + 1;                * @param nums * @param target * @return */public static int BinarySearch (int[] nums, int target) {        int left = 0;                int right = Nums.length-1;            while (left <= right) {int mid = (left + (Right-left)/2);                        if (target = = Nums[mid]) {while (Mid >= 0) {if (Nums[mid]! = target) {                    Break  } mid--;              } if (Mid <=-1) {return 0; } return mid + 1;//minus once, return with 1}else if (Target < Nums[mid]) {right = M            Id-1;            }else {left = mid + 1;    }} return-1; }

Binary lookup algorithm for finding duplicate elements

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.