谁帮我用java写一个最简单的输入数字,然后进行二分查找法啊!!谢谢了!!

解决方案 »

  1.   

    最起码你也google或者baidu一下了再问啊
      

  2.   

      private int recFind(long searchKey, int lowerBound,
                                             int upperBound)
          {
          int curIn;      curIn = (lowerBound + upperBound ) / 2;
          if(a[curIn]==searchKey)
             return curIn;              // found it
          else if(lowerBound > upperBound)
             return nElems;             // can't find it
          else                          // divide range
             {
             if(a[curIn] < searchKey)   // it's in upper half
                return recFind(searchKey, curIn+1, upperBound);
             else                       // it's in lower half
                return recFind(searchKey, lowerBound, curIn-1);
             }  // end else divide range
          }  // end recFind()
      

  3.   

    刚好我手头有一个,给你看看
    package fx.algorithm;/**
     * 二分查找(针对一个有序数组) ,每次搜索取一半的范围
     * 
     * @author 咖啡
     * 
     */
    public class BinaryFind { private static int[] array = { 13, 27, 38, 49, 65, 76, 97, 88 }; public static void main(String[] args) {
    System.out.println(search(38));
    } private static int search(int searchKey) {
    int lower = 0;
    int upper = array.length - 1; while (true) {
    int curIndex = (lower + upper) / 2; if (searchKey == array[curIndex])
    return curIndex;
    else if (lower > upper)
    return -1;
    else {
    if (searchKey > array[curIndex])
    lower = curIndex + 1;
    else
    upper = curIndex - 1;
    }
    } }
    }
      

  4.   

    都1点了,还帮你写了个,给点分吧!public class binarySearch { public static int Max = 20;
    public static int[] Data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20 }; public static void main(String[] args) {
    int weizhi = BinarySearch(8);
    System.out.println(weizhi);
    } public static int BinarySearch(int num) {
    int L; // left
    int R; // right
    int M; // middle
    int position = 0; L = 0;
    R = Max - 1;
    while (L <= R) {
    M = (L + R) / 2;
    if (num < Data[M])
    R = M - 1;
    else if (num > Data[M])
    L = M + 1;
    else if (num == Data[M]) {
    position = M;
    return position;
    }
    }
    return position;
    }}