Binary search of a sorted array

前端 未结 6 975
南方客
南方客 2021-02-04 10:04

I am trying to search a descending sorted array using this binary search code. However, after I sort it, and try to search, it doesn\'t come back with any result, just a loading

6条回答
  •  余生分开走
    2021-02-04 11:03

    public static object BinnarySearch(int[] array,int SearchKey)
        {
            int min = 0;
            int max = array.Length - 1;
            while (min <= max)
            {
                int mid = (min + max) / 2;
                if (SearchKey == array[mid])
                {
                    return mid;
                }
                else if (SearchKey < array[mid])
                {
                    max = mid - 1;
                }
                else
                    min = mid + 1;
            }
            return "Not Found";
        }
    

提交回复
热议问题