GIS之家

愿大家在GIS的海洋里自由遨游

导航

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

Posted on 2010-05-27 23:41  雨衣blog  阅读(855)  评论(0编辑  收藏  举报

        private static int find(int[] arySource, int target, int start, int end)
        {
            if (start == end)
            {
                if (arySource[start] == target)
                {
                    return start;
                }
                else
                {
                    return -1;
                }
            }
            else if (start > end)
            {
                return -1;
            }

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

            int middleValue = arySource[curIndex];
            if (target == middleValue)
            {
                return curIndex;
            }
            else if (target < middleValue)
            {
                return find(arySource, target, start,  curIndex - 1 );
            }
            else
            {
                return find(arySource, target,  curIndex + 1, end);
            }
        }