⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 halfsearch.java

📁 java程序实现的通俗易懂的二分查找的算法源代码
💻 JAVA
字号:
  public class halfSearch
  {
      public static int bsearch(int array[],int value)
      {
        boolean found=false;
        int high=array.length-1;
        int low=0;
        int cnt=0;//查找步数
        int mid=(high+low)/2;
        System.out.println("Looking for "+value);
        while(!found&&(high>=low))
        {
           System.out.print(" Low "+low+" Mid "+mid);
           System.out.print(" High "+high);
           if(value==array[mid])
               found=true;
           else
               if(value< array[mid])
                  high=mid-1;
               else
                  low=mid+1;
           mid=(high+low)/2;
           cnt++;
         }
         System.out.println();
         System.out.println("Steps "+cnt);
         return((found)?mid:-1);
       }

     public static void main(String[] args)
     {
        int array[]=new int[100];
        for(int i=0;i< array.length;i++)
             array[i]=i;
        System.out.println("Resulte "+bsearch(array,67));
   //     System.out.println("Resulte "+bsearch(array,33));
   //     System.out.println("Resulte "+bsearch(array,1));
   //     System.out.println("Resulte "+bsearch(array,1001));        
     }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -