唉,二分法都乱copy,世道变了啊~~

     /**
     * 二分法查找
     */
     int a[] = { 110, 120, 140, 23, 45, 98, 100, 70 };
     int search = 145; // 记录要查找的元素
     int lower = 0; // 记录第一个元素
     int temp = a.length - 1;
     int index = -1;
     while ( true) {
      System.out.println( "lo>" + lower + "\tte>" + temp);
       if (lower == temp) {
        index = lower;
         break;
      } else if (a[lower] == search) {
        index = lower;
         break;
      } else if (a[temp] == search) {
        index = temp;
         break;
      } else {
        lower++;
        temp--;
        index++;
         if (lower > temp) {
           break;
        }
      }
    }
     if (lower <= temp) {
      System.out.println(search + "在数组中第:" + (index + 1) + "位");
    } else {
      System.out.println( "里面没有这个元素");
    }
  
留给历史吧。

你可能感兴趣的:(二分法)