二分法快速查找的递归算法

http://www.cnblogs.com/darejoy/archive/2010/05/27/1745871.html


  1. private static int find(int[] arySource, int target, int start, int end)
  2. {
  3.     if (start == end)
  4.     {
  5.         if (arySource[start] == target)
  6.         {
  7.             return start;
  8.         }
  9.         else
  10.         {
  11.             return -1;
  12.         }
  13.     }
  14.     else if (start > end)
  15.     {
  16.         return -1;
  17.     }

  18.     int curIndex = -1;
  19.     curIndex = (start + end) / 2;

  20.     int middleValue = arySource[curIndex];
  21.     if (target == middleValue)
  22.     {
  23.         return curIndex;
  24.     }
  25.     else if (target < middleValue)
  26.     {
  27.         return find(arySource, target, start, curIndex - 1 );
  28.     }
  29.     else
  30.     {
  31.         return find(arySource, target, curIndex + 1, end);
  32.     }
  33. }

阅读(1976) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~
评论热议
posted on 2016-01-25 16:45  玄冬  阅读(245)  评论(0编辑  收藏  举报