Binary search with addition and subtraction

Keywords: Java

Recently, I was studying Algorithms 4th, making a record.

subject

Binary search with only addition and subtraction. [Mihai Patrascu] Write a program that, given an array of N distinct int values in ascending order, determines whether a given integer is in the array. You may use only additions and subtractions and a constant amount of extra memory. The running time of your program should be proportional to log N in the worst case.
Dichotomy lookup using addition and subtraction only. Write a program given an array with N different int values and sorted in ascending order to determine whether there are given integers. It can only use addition and subtraction as well as extra memory. In the worst case, the running time of the program should be proportional to logN.

Solution

package day1;

import edu.princeton.cs.algs4.*;

import java.util.Arrays;
import java.util.HashSet;

public class Mihai_Patrascu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = getArr(20000);
        StdOut.println();
        Arrays.sort(arr); // sort
        for (int i :arr){
            StdOut.print(i+" ");
        }
//      fbnq(10, 5, 2);
        int index = max(arr, 0, arr.length, 10);
        StdOut.println("answer: "+index);
    }
    /**
     * 
     * @param arr array
     * @param lo Start subscript
     * @param hi Final subscript
     * @param goal Finding parameters
     * @return
     */
    public static int  max(int[] arr, int lo, int hi, int goal) {
        if (lo > hi) return -1;
        // init 
        if (hi - lo <= 2) {
            if (arr[lo] == goal) return lo;
            if (arr[hi] == goal) return hi;
            else {
                return -1;
            }
        }else {

            int k3 = 0;
            int i = 3;
            while (k3 < hi) {
                i++;
                k3 = fbnq(i, lo,  1) - lo;
                StdOut.print(k3 + " ");
            }
            StdOut.println();

            int k2 = fbnq(i - 1, 0, 1) + lo;
            if (arr[k2] > goal) return max(arr, lo, k2 - 1, goal);
            if (arr[k2] < goal) return max(arr, k2 + 1, hi, goal);
            else return k2;
        }


    }

    // Fibonacci sequence
    /**
     * 
     * @param n subscript
     * @param first First number
     * @param second Second numbers
     * @return n Numbers under small scales
     */
    public static int fbnq(int n,int first, int second) {
        int a = first;
        int b = second;
        if (n < 3) return 1;
        else {
            int i = 3;
            while (i <= n) {
                int temp ;
                temp = a;
                a = b;
                b = temp + a;
                i++;
//              StdOut.print(b+" ");
            }
            return b;
        }
    }

    // Create non-repetitive arrays
    public static int[] getArr(int arrSize) {
        HashSet< Integer> set = new HashSet<>();
        int[] arr = new int[arrSize];
        for (int i = 0; i<arrSize; i++){
            int a = StdRandom.uniform(arrSize * 2); // It may take a little more time to recreate, but it helps to find the corresponding number.
            int count = set.size();
            set.add(a);
            if (count < set.size()) {
                arr[i] = a;
            }else {
                i--;
            }
        }
        return arr;
    }

}

Last

stakoverflow has a question about who's fast in Fibonacci search and binary search. The respondents don't know much about hardware.
Attach a link
It's not easy to simplify the verification. If you have any mistakes, you can contact me and correct them in time.

Posted by linda_v on Tue, 12 Feb 2019 20:54:18 -0800