算法C#

发布时间 2023-04-08 14:59:28作者: zjp971014
     #region 二分查找法
        public static int BinarySertch(int[] arr, int startIndex, int endIndex, int result) 
        {
            if (startIndex > endIndex) 
            {
                return -1;
            }

            int midIndex = (endIndex - startIndex) / 2 + startIndex;
            if (result > arr[midIndex])
            {
                return BinarySertch(arr, midIndex + 1, endIndex, result);
            } 
            else if (result < arr[midIndex])
            {
                return BinarySertch(arr, startIndex, midIndex - 1, result);
            }
            else 
            {
                return arr[midIndex];
            }
        }
        #endregion