首页 > 其他 > 详细

【笔记】JAVA二分法查找

时间:2014-02-04 03:47:10      阅读:310      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
public class Array
{
   
  public static int binarySearch(int[] array,int value)
  {
      int low=0;
      int high=array.length-1;
      int middle;
      while(low<=high)
      {
          middle=(low+high)/2;
          for(int i=0;i<array.length;i++)
          {
              System.out.print(array[i]);
          if(i==middle)
          {
              System.out.print("#");
          }
          System.out.print(" ");
          }
          System.out.println();
           
          if(array[middle]==value)
          {
              return middle;
          }
          if(value<array[middle])
          {
              high=middle-1;
          }
          if(value>array[middle])
          {
              high=middle+1;
          }
      }
      return -1;
      
  }
    
  
  public static void main(String[] args)
  {
      int[] a =new int []{1,2,3,4,5,6,7,8,9,};
      int value=6;
      int index=binarySearch(a,value);
      System.out.println(index);
  }
  
}
bubuko.com,布布扣

可是输出结果。。怎么会这样、、、

bubuko.com,布布扣
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
1 2# 3 4 5 6 7 8 9 
bubuko.com,布布扣

【笔记】JAVA二分法查找

原文:http://www.cnblogs.com/jiahanying/p/3537702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!