A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mishisanyi 中级黑马   /  2015-6-17 16:21  /  604 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /**
  2. *
  3. */
  4. package array;

  5. /**
  6. * @author mishi
  7. *
  8. */
  9. public class Bisearch {

  10.         /**
  11.          * @param args
  12.          */
  13.         public static void main(String[] args) {
  14.                 // TODO 自动生成的方法存根
  15.                 int[] array = {1,3,5,7,9,11,14,15,16,17,20};
  16.                 System.out.println(bisearch(array,0,array.length,20));
  17.         }
  18.         public static int bisearch(int array[],int fromIndex,int endIndex,int num)
  19.         {
  20.                 int half = (fromIndex + endIndex)/2;
  21.                 while(half!=fromIndex&&half!=endIndex)
  22.                 {
  23.                         if(num<array[half])
  24.                         {
  25.                                 return bisearch(array, fromIndex, half, num);
  26.                         }
  27.                         else if(num>array[half]) {
  28.                                 return bisearch(array, half+1, endIndex, num);
  29.                         }
  30.                         else
  31.                                 return half;
  32.                 }
  33.                 if(num == array[half])
  34.                         return half;
  35.                 return -1;
  36.         }

  37. }
复制代码

6 个回复

倒序浏览
不错不错哦。学习中。。。

评分

参与人数 1黑马币 +1 收起 理由
mishisanyi + 1 赞一个!

查看全部评分

回复 使用道具 举报
留一下  学学

评分

参与人数 1黑马币 +1 收起 理由
mishisanyi + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 qq2403956244 于 2015-6-17 19:33 编辑

  1. /**
  2. 折半查找 --->英文单词:bisearch
  3. @author q...4
  4. */
  5. /*
  6. 1.需求:改进楼主代码

  7. 2.思路:发现,楼主代码中有如下问题
  8. a.while循环,每执行一次循环都要调用一次本身,假如数很多效率低。
  9. b.循环条件需稍作修改。
  10. c.当 num >20时,比如 num=23 ,这时就会报错ArrayIndexOutOfBoundsException。

  11. 3.步骤:a.用一个while循环就可以完成查找。
  12. b.循环条件只需 头角标(索引) <= 尾角标。
  13. c.尾角标 = 数组.长度 - 1。
  14. */
  15. public class Bisearch
  16. {

  17. /**
  18. * @param args
  19. */
  20. //主函数
  21.     public static void main(String[] args)
  22.     {
  23.              // TODO 自动生成的方法存根
  24.       int[] array = {1,3,5,7,9,11,14,15,16,17,20};
  25.                   // 0 1 2 3 4 5 6 7 8 9 10 角标
  26.       System.out.println("Index="+bisearch(array,0,array.length-1,20));
  27.     }

  28.    //定义一个方法判断需要查找的元素是否在数组中,是->直接返回结果,不是->返回-1 。(角标没有负数)
  29.     public static int bisearch(int array[],int fromIndex,int endIndex,int num)
  30.     {

  31.         while(fromIndex <= endIndex)    //half!=fromIndex&&half!=endIndex
  32.         {
  33.            int half = (fromIndex + endIndex)/2;   //中角标。5,8,9,10

  34.         if(num<array[half])     //20<11,20<16,20<17,20<20
  35.        {
  36.            endIndex = half-1;    //return bisearch(array, fromIndex, half, num);

  37.        }
  38.        else if(num>array[half]) {    //20>11,20>16,20>17

  39.           //return bisearch(array, half+1, endIndex, num);
  40.             fromIndex = half+1;      //6,9,10
  41.        }
  42.       else
  43.        {
  44.               return half;   //10
  45.        }
  46.     }
  47.       //if(num == array[half])
  48.        // return half;
  49.        return -1;
  50.     }
  51. }
复制代码



回复 使用道具 举报
学了就忘了

评分

参与人数 1黑马币 +1 收起 理由
mishisanyi + 1

查看全部评分

回复 使用道具 举报
谢谢大家的支持,楼上的我都加了黑马分,谢谢啦
回复 使用道具 举报
yzzojf 中级黑马 2015-6-18 14:58:54
7#
留存,学习,辛苦了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马