二分查找法 C#实现

复习二分查找法。


public int FindPosition(int num, int[] arr)
        {
            int left = 0;
            int right = arr.Length - 1;


            while (left < right - 1)
            {
                if (arr[left] == num)
                {
                    return left;
                }
                if (arr[right] == num)
                {
                    return right;
                }


                int middle = (left + right) / 2;
                if (num == arr[middle])
                {
                    return middle;
                }
                else if (num < arr[middle])
                {
                    right = middle;
                }
                else
                {
                    left = middle;
                }


            }
            return -1;
        }


你可能感兴趣的:(C#)