Super basic algorithm: binary search method

Introduction

Dichotomy is one of the divide and conquer algorithms among the five basic algorithms. According to the idea of ​​divide and conquer, the idea of ​​dichotomy is very simple, namely:

  1. Divide the interval into two parts and determine which part the search element belongs to.
  2. Update the interval and repeat the first step. Until the interval is reduced to only one element.

The binary search method has relatively large limitations in actual use. The binary search method must comply with the following requirements:

  1. Must use sequential storage structure , such as array or linked list. Collection is not acceptable.
  2. Must be arranged in order, that is, they are arranged in order when stored.

For example

For example: a 10-digit array, store any 10 letters from a to z in order, which contains the letter x. Find the letter x according to the dichotomy.

a c d f j l n q x with

First find the element j in the middle of the array (j or l is fine, take j as an example), j is less than x, and the elements are stored in the array in order, then x should be between j and z.

  1. Take the element n between j and z, and n is still less than x.
  2. Take the element q between n and z, and q is less than x.
  3. Take the element in the middle of q~z and find x.

The above is a simple example of binary search. If you use code implementation, you need to use recursion. The difficulty lies in:

  • How to determine the termination conditions;
  • When there are two medians, which one shall be taken;
  • How to get the correct value when there are only two elements in the last interval;

application

Here is an example:

Find the number of duplicates:

Given an array nums containing n + 1 integers, whose numbers are all between 1 and n (including 1 and n), we know that there is at least one repeated integer. Assuming there is only one repeated integer, find the repeated number.

Example 1:

输入: [1,3,4,2,2]
输出: 2

Source: LeetCode
Link: Original title link

Ideas:

The following conditions can be extracted from the title:

  • There is only one repeated number;
  • All numbers are in the range of n;
  • There are a total of n+1 elements;

The question only requires finding the value of the repeated element, and does not care about the position of the element, so you can use the dichotomy to find it.

Because all elements are between 1 and n, and only one element is repeated, assuming there is no such repeated number, then the number of elements less than n/2 in this array should be the same as the number of elements greater than n/2 . If you add this number, the balance is broken, and you can judge whether the number belongs to an interval greater than n/2 or an interval less than n/2.

Iterate with this as the key, and finally get the value of this repeated element.

Code:

package leetcode;


public class FindDuplicate {
    int num = 0;
    public int findDuplicate(int[] nums) {
        int minLine = 1;
        int maxLine = nums.length-1;
        dichotomySearch(minLine,maxLine,nums);
        return num;
    }

    public void dichotomySearch(int minLine,int maxLine,int[] nums) {
        int flag = 0;
        int median = (minLine + maxLine)/2;

        //only two number
        if(minLine==maxLine-1||minLine==maxLine){
            for (int i = 0; i < nums.length; i++) {
                if (nums[i]==minLine){
                    flag++;
                }
            }
            if (flag>1){
                num = minLine;
            }else {
                num = maxLine;
            }
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < median && nums[i]>=minLine){
                flag--;
            }
            if (nums[i] > median && nums[i]<=maxLine){
                flag++;
            }
        }
        if (flag<=0){
            maxLine = median;
            dichotomySearch(minLine,maxLine,nums);
        }else {
            minLine = median;
            dichotomySearch(minLine,maxLine,nums);
        }
    }

    public static void main(String[] args) {
        int[] nums =  {1,1};
        FindDuplicate findDuplicate = new FindDuplicate();
        System.out.println(findDuplicate.findDuplicate(nums));
    }
}

 

Guess you like

Origin blog.csdn.net/x950913/article/details/106361889
Recommended