4.在一个有序数组中,找某个数是否存在(二分法)

bool IsExistTarget(int arr[], int size, int target)
{
  if (!arr) return false;
    
   int Left = 0;
   int Right = 0;
   int Mid = 0;
    
    while (Left < Right)
    {
    	Mid = Left + ((Right-Left)>>1);
        
        if (arr[Mid] == target)
        {
        	return true;
        }
        else if (arr[mid] > target)
        {
        	Right = mid - 1;
        }
        else
        {
        	Left = mid + 1;
        }
    }
    
    return target == arr[Left];
}
posted @ 2022-09-08 11:04  test369  阅读(28)  评论(0编辑  收藏  举报