Java binary lookup code demonstration

From , 4 Years ago, written in Java, viewed 53 times.
URL https://pastebin.vip/view/3c550e0c
  1.    
  2.     //二分查找法
  3.    
  4.     class Find
  5.    
  6.     {
  7.    
  8.     public void find(int liftIndex,int reghtIndex,int val,int arr[]
  9.    
  10.     {
  11.    
  12.     /*
  13.    
  14.     * liftIndex 最左边的下标
  15.    
  16.     * reghtIndex 最右边的下标
  17.    
  18.     * val 要查找的数
  19.    
  20.     * midIndex 中间数的下标
  21.    
  22.     */
  23.    
  24.     int midIndex=(liftIndex+reghtIndex)/2;
  25.    
  26.     if(liftIndex<=reghtIndex)
  27.    
  28.     {
  29.    
  30.     //比较
  31.    
  32.     //如果要查找的数比中间的数大则接下来在右边找
  33.    
  34.     if(val>arr[midIndex]
  35.    
  36.     {
  37.    
  38.     find(midIndex+1,reghtIndex,val,arr);
  39.    
  40.     }
  41.    
  42.     //如果要查找的数比中间的数小则接下来在左边找
  43.    
  44.     else if(val<arr[midIndex]
  45.    
  46.     {
  47.    
  48.     find(liftIndex,midIndex-1,val,arr);
  49.    
  50.     }
  51.    
  52.     //如果刚好相等,则找到了
  53.    
  54.     else if(val==arr[midIndex]
  55.    
  56.     {
  57.    
  58.     System.out.println"找到"+val+"了,是第"+(midIndex+1+"个数字");
  59.    
  60.     }
  61.    
  62.     }
  63.    
  64.     }
  65.    
  66.     }
  67. //java/4485

Reply to "Java binary lookup code demonstration"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website