Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created May 29, 2024 17:56
Show Gist options
  • Save shitu13/c9efbd4eea2aa34c502a8c74b9b2de74 to your computer and use it in GitHub Desktop.
Save shitu13/c9efbd4eea2aa34c502a8c74b9b2de74 to your computer and use it in GitHub Desktop.
Implementation of binary search with loop
int search(vector<int> &nums, int target) {
// Write your code here.
int start =0;
int end = nums.size()-1;
int mid = start+(end-start)/2;
while(start<=end){
if(nums[mid]==target)
return mid;
else if(target<nums[mid])
end = mid-1;
else
start = mid + 1;
mid = start+(end-start)/2;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment