Algorithm | dichotomy

Time:2021-12-12

In data structure books, the precondition of order is often added when introducing dichotomy. The definition of order is either from large to small or from small to large.

So, is order really a necessary condition for using dichotomy in solving all problems?
The answer is: No

As long as the elimination logic on the left and right sides is correctly constructed, you can use the dichotomy. Now let’s understand the dichotomy through the following examples.

1. In an ordered array, find whether a number exists
2. In an ordered array, find the leftmost position of > = a number
3. In an ordered array, find the rightmost position of < = a number
4. Local minimum problem

In an ordered array, find whether a number exists

public static boolean exist(int[] sortedArr, int num) {
        if (sortedArr == null || sortedArr.length == 0) {
            return false;
        }
        int L = 0;
        int R = sortedArr.length - 1;
        int mid = 0;
        // L.. When R is at least two numbers
        while (L < R) { 
            mid = L + ((R - L) >> 1);
            if (sortedArr[mid] == num) {
                return true;
            } else if (sortedArr[mid] > num) {
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return sortedArr[L] == num;
    }
Algorithm | dichotomy

In an ordered array, find whether a number exists png

In an ordered array, find the leftmost position greater than or equal to a number

public static int nearestIndex(int[] arr, int value) {
        int L = 0;
        int R = arr.length - 1;
        int index = -1; //  Record the leftmost check mark
        While (L < = R) {// when there is at least one number
            int mid = L + ((R - L) >> 1);
            if (arr[mid] >= value) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }
Algorithm | dichotomy

In an ordered array, find the leftmost position greater than or equal to a number png

In an ordered array, find the rightmost position of a number less than or equal to

public static int nearestIndex(int[] arr, int value) {
        int L = 0;
        int R = arr.length - 1;
        int index = -1; //  Record the rightmost check mark
        while (L <= R) {
            int mid = L + ((R - L) >> 1);
            if (arr[mid] <= value) {
                index = mid;
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        return index;
    }
Algorithm | dichotomy

In an ordered array, find the rightmost position of a number less than or equal to png

Local minimum problem

public static int getLessIndex(int[] arr) {
        if (arr == null || arr.length == 0) {
            return -1; // no exist
        }
        if (arr.length == 1 || arr[0] < arr[1]) {
            return 0;
        }
        if (arr[arr.length - 1] < arr[arr.length - 2]) {
            return arr.length - 1;
        }
        int left = 1;
        int right = arr.length - 2;
        int mid = 0;
        while (left < right) {
            mid = (left + right) / 2;
            if (arr[mid] > arr[mid - 1]) {
                right = mid - 1;
            } else if (arr[mid] > arr[mid + 1]) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return left;
    }

The local minimum can be achieved by dichotomy. The implicit law when defining the local minimum is that there must be a local minimum for an array that does not have the same elements, because only one local minimum needs to be found, so the binary search can be used to gradually reduce the size of the array.
1. First, judge whether the first value of the array is less than the value on the right. If less than, the local minimum value is the first number; Otherwise, proceed to the next step;
2. Judge whether the last value of the array is less than the value on the left. If less than, the local minimum value is the last number; Otherwise, proceed to the next step;
3. Verify the local minimum value of array data except the first and last positions by dichotomy. If the values on the left and right sides of a value are greater than the value, the value belongs to the local minimum value; Otherwise, continue to search through dichotomy.