GIS之家

愿大家在GIS的海洋里自由遨游
随笔 - 54, 文章 - 1, 评论 - 17, 阅读 - 49682

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   雨衣blog  阅读(857)  评论(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);
            }
        }

点击右上角即可分享
微信分享提示