Created
May 29, 2024 17:56
-
-
Save shitu13/c9efbd4eea2aa34c502a8c74b9b2de74 to your computer and use it in GitHub Desktop.
Implementation of binary search with loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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