Binary search if array contains duplicates

随声附和 提交于 2019-12-04 21:14:57

The array you proposed has the target value in the middle index, and in the most efficient implementations will return this value before the first level of recursion. This implementation would return '5' (the middle index).

To understand the algorithm, just step through the code in a debugger.

public class BinarySearch {
    public static int binarySearch(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = left + (right-left) / 2;
          if (array[middle] == value)
                return middle;
          else if (array[middle] > value)
                return binarySearch(array, value, left, middle - 1);
          else
                return binarySearch(array, value, middle + 1, right);           
    }

    public static void main(String[] args) {
        int[] data = new int[] {10,20,21,24,24,24,24,24,30,40,45};

        System.out.println(binarySearch(data, 24, 0, data.length - 1));
    }
}
rishabh

As pointed out by @Pleepleus it will return the index 5 from the first level of recursion itself. However I would like to point out few things about binary search :

  1. Instead of using mid = (left + right)/2 , use mid = left + (right-left)/2
  2. If you want to search for lower_bound or upper_bound of an element use the following algorithms:

    binLowerBound(a, lo, hi, x)
      if (lo > hi)
        return lo;
    
      mid = lo +  (hi - lo) / 2;
      if (a[mid] == x)
        return binLowerBound(a, lo, mid-1, x);
      else if (a[mid] > x)
        return binLowerBound(a, lo, mid-1, x);
      else
        return binLowerBound(a, mid+1, hi, x);
    
    binHigherBound(a, lo, hi, x)
      if (lo > hi)
        return lo;
      mid = lo + (hi - lo) / 2;
      if (a[mid] == x)
        return binHigherBound(a, mid+1, hi, x);
      else if (a[mid] > x)
        return binHigherBound(a, lo, mid-1, x);
      else
        return binHigherBound(a, mid+1, hi, x);
    
public class a{
    public static int binarySearch(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = (left + right) / 2;
          if (array[middle] == value)
        {
            if(array[middle-1]<array[middle])
                return middle;
                 //return binarySearch(array, value, left, middle - 1);
                 else
                return binarySearch(array, value, left, middle - 1);
        }
          else if (array[middle] > value)
                return binarySearch(array, value, left, middle - 1);
          else
                return binarySearch(array, value, middle + 1, right);           
    }
public static int binarySearch1(int[] array, int value, int left, int right) {
          if (left > right)
                return -1;
          int middle = (left + right) / 2;
          if (array[middle] == value)
        {
            if(array[middle]<array[middle+1])
                return middle; 
                 else

                    return binarySearch1(array, value, middle + 1, right);           
        }
          else if (array[middle] > value)
                return binarySearch1(array, value, left, middle - 1);
          else
                return binarySearch1(array, value, middle + 1, right);           
    }

    public static void main(String[] args) {
        int[] data = new int[] {10,20,21,24,24,24,24,24,30,40,45};


        System.out.println(binarySearch(data, 24, 0, data.length - 1));     //First Index
        System.out.println(binarySearch1(data, 24, 0, data.length - 1));    //Last Index
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!