adding the binary search code

This commit is contained in:
Jidong Xiao
2023-10-09 01:07:29 -04:00
parent fd8719bad5
commit 64dcfc583b
3 changed files with 85 additions and 1 deletions

View File

@@ -136,4 +136,4 @@ a recursive call is made.
## 12.10 Leetcode Exercises
- [Leetcode problem 704: Binary Search](https://leetcode.com/problems/binary-search/). Solution: [p141_linkedlistcycle.cpp](../../leetcode/p141_linkedlistcycle.cpp)
- [Leetcode problem 704: Binary Search](https://leetcode.com/problems/binary-search/). Solution: [p704_binarysearch.v1.cpp](../../leetcode/p704_binarysearch.v1.cpp)a or [p704_binarysearch.v2.cpp](../../leetcode/p704_binarysearch.v2.cpp)

View File

@@ -0,0 +1,52 @@
class Solution {
public:
bool search(vector<int>& nums, int low, int high, int target, int& index){
// this is better than mid=(high+low)/2 because the latter might overflow when high and low are very large integers.
// caution: if low = high - 1, then mid = low. and this could lead to an infinite loop situation. and because of this, we should never search the range [mid, high], because that actually means search the range of [low, high], which is a repeat of the existing call.
int mid = low + (high-low)/2;
// base case
if(low>=high){
index = low;
return (target == nums[low]);
}
// general case
if(target<=nums[mid]){
// search the first half
// and because now we need to pass the range, we have to write another function.
return search(nums, low, mid, target, index);
}else{
// search the second half
return search(nums, mid+1, high, target, index);
}
}
int search(vector<int>& nums, int target) {
int index;
int size = nums.size();
// code below should go into the other search() function, but I write the code like this so as to help you to see how the idea of creating the other search() function was formed.
int mid = size/2;
if(size == 1){
if(target == nums[0]){
return 0;
}else{
return -1;
}
}
if(target<=nums[mid]){
// search the first half
// because now we need to pass the range, we have to write another function.
if(search(nums, 0, mid, target, index) == true){
return index;
}else{
return -1;
}
}else{
// search the second half
if(search(nums, mid+1, size-1, target, index) == true){
return index;
}else{
return -1;
}
}
}
};

View File

@@ -0,0 +1,32 @@
class Solution {
public:
bool search(vector<int>& nums, int low, int high, int target, int& index){
// this is better than mid=(high+low)/2 because the latter might overflow when high and low are very large integers.
// caution: if low = high - 1, then mid = low. and this could lead to an infinite loop situation. and because of this, we should never search the range [mid, high], because that actually means search the range of [low, high], which is a repeat of the existing call.
int mid = low + (high-low)/2;
// base case
if(low>=high){
index = low;
return (target == nums[low]);
}
// general case
if(target<=nums[mid]){
// search the first half
// and because now we need to pass the range, we have to write another function.
return search(nums, low, mid, target, index);
}else{
// search the second half
return search(nums, mid+1, high, target, index);
}
}
// the driver function
int search(vector<int>& nums, int target) {
int index;
int size = nums.size();
if(search(nums, 0, size-1, target, index) == true){
return index;
}else{
return -1;
}
}
};