package TestErfenCha;
public class TestErfenCha {
public static void search(double shu, double[] arr){
int len = arr.length-1;
int min = 0, max = len, mid = (max-min)/2;
int count = 1;
System.out.println("第"+count+"次的最小值是:"+arr[min]);
System.out.println("第"+count+"次的中间是:"+arr[mid]);
System.out.println("第"+count+"次的最大值是:"+arr[max]);
count++;
while(shu!=arr[mid]){
if(shu>arr[max]){
System.out.println("对不起,数不存在");
break;
}
if(shu<arr[mid]){
min = min;
max = mid-1;
mid = min+(max-min)/2;
System.out.println("第"+count+"次的最小值是:"+arr[min]);
System.out.println("第"+count+"次的中间是:"+arr[mid]);
System.out.println("第"+count+"次的最大值是:"+arr[max]);
if(mid<min){
System.out.println("对不起,没有找到");
break;
}
}
else if(shu>arr[mid]){
min = mid+1;
max = max;
mid = min+(max-min)/2;
System.out.println("第"+count+"次的最小值是:"+arr[min]);
System.out.println("第"+count+"次的中间是:"+arr[mid]);
System.out.println("第"+count+"次的最大值是:"+arr[max]);
if(mid<min){
System.out.println("对不起,没有找到");
break;
}
}

if(shu==arr[mid]){
System.out.println("恭喜你找到元素");
break;

}
count++;

}
}



public static void main(String[] args){
double arr[] = new double[15];
for(int i=0;i<15;i++){
arr[i] = i+3;
System.out.print(arr[i]+" ");
}
System.out.println();
search(7.69,arr);
}
}3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 
第1次的最小值是:3.0
第1次的中间是:10.0
第1次的最大值是:17.0
第2次的最小值是:3.0
第2次的中间是:6.0
第2次的最大值是:9.0
第3次的最小值是:7.0
第3次的中间是:8.0
第3次的最大值是:9.0
第4次的最小值是:7.0
第4次的中间是:7.0
第4次的最大值是:7.0
对不起,数不存在经过修改,已经避免了bug;不过如果还有其他bug,欢迎广大朋友检举,谢谢了;

解决方案 »

  1.   

      System.out.println("恭喜您做对了");
      

  2.   

    欢迎有其他方法的朋友一起来分享代码;如果本例有bug,希望朋友们提出好建议;
      

  3.   

    double[] arr={1,2,3,4};
    search(0, arr); //数组越界
    search(2, arr); //无任何提示
      

  4.   

    public static void main(String args[]) {
    HashSet<Integer> hs = new HashSet<Integer>();
    while (hs.size() < 100) {
    hs.add((int) (Math.random() * 500));
    }
    // 转换为int
    int al[] = new int[100];
    int p = 0;
    for (Integer i : hs) {
    al[p] = i.intValue();
    p++;
    }
    // 冒泡排序
    boolean b = true;
    while (b) {
    b = false;
    for (int i = 0; i < 99; i++) {
    if (al[i] > al[i + 1]) {
    int tmp = al[i];
    al[i] = al[i + 1];
    al[i + 1] = tmp;
    b = true;
    }
    }
    } // 查找 ;二分查找法 ;
    int sss = 234; // 查找的数234
    int maxIndex = 99;
    int minIndex = 0;
    int index = (maxIndex - minIndex) / 2;//center
    b = false;
    while (minIndex < (maxIndex - 1)) {
    if (sss > al[index]) {
    minIndex = index;
    index = minIndex + (maxIndex - minIndex) / 2;
    }
    if (sss < al[index]) {
    maxIndex = index;
    index = maxIndex - (maxIndex - minIndex) / 2;
    }
    if (sss == al[index]) {
    b = true;
    break;
    }
    }
    for (int i = 0; i < al.length; i++) {
    System.out.print(al[i] + "\t");
    }
    if (b) {
    System.out.println("找到了" + (index + 1) + "对应的值" + al[index]);
    } else {
    System.out.println("没找到");
    } }
      

  5.   


    public class BinarySearch
    {
        public static final int NOT_FOUND = -1;
        
        /**
         * Performs the standard binary search
         * using two comparisons per level.
         * @return index where item is found, or NOT_FOUND.
         */
        public static <AnyType extends Comparable<? super AnyType>>
                      int binarySearch( AnyType [ ] a, AnyType x )
        {
            int low = 0;
            int high = a.length - 1;
            int mid;        while( low <= high )
            {
                mid = ( low + high ) / 2;            if( a[ mid ].compareTo( x ) < 0 )
                    low = mid + 1;
                else if( a[ mid ].compareTo( x ) > 0 )
                    high = mid - 1;
                else
                    return mid;
            }        return NOT_FOUND;     // NOT_FOUND = -1
        }    // Test program
        public static void main( String [ ] args )
        {
            int SIZE = 8;
            Integer [ ] a = new Integer [ SIZE ];
            for( int i = 0; i < SIZE; i++ )
                a[ i ] = i * 2;        for( int i = 0; i < SIZE * 2; i++ )
                System.out.println( "Found " + i + " at " +
                                     binarySearch( a, i ) );    }
    }