Binary search works by repeatedly dividing the array into two halves that can contain the given target element, till the search area is limited to just one element. The necessary condition for binary search to work is the array needs to be sorted
Given a sorted array, find if element 2 is present in the array.
Step 1: The given array:
Step 2: Consider two pointers, low = 0 and high = N - 1, where N is the size of the array.
Step 3: Find the mid, i.e. mid = (low + high) / 2. Here, mid = (0 + 6) / 2 = 3.
Step 4: Now, check for three conditions:
Step 5: If A[mid] < 2, shift low = mid + 1.
Step 6: Repeat Steps 3 to Step 5, until the search area is limited to just one element i.e. low = high.
Step 7: The value 2 is found.
Binary Search can be implemented in mainly two ways:
Let us first discuss the Recursive method.
int bSearch(int arr[], int left, int right, int target) {
if (right >= left) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, right, target);
}
return -1;
}
class BinarySearch {
int bSearch(int arr[], int left, int right, int target) {
if (right >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
}
def bSearch(arr, left, right, target):
if right >= left:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binarySearch(arr, left, mid-1, target)
else:
return binarySearch(arr, mid + 1, right, target)
else:
return -1
Time Complexity :
int bSearch(int arr[], int left, int right, int target) {
while (left <= right) {
int m = left + (right - left) / 2;
if (arr[m] == target)
return m;
if (arr[m] < target)
left = m + 1;
else
right = m - 1;
}
return -1;
}
class BinarySearch {
int bSearch(int arr[], int left, int right, int target)
{
int left = 0, right = arr.length - 1;
while (left <= right) {
int m = left + (right - left) / 2;
if (arr[m] == target)
return m;
if (arr[m] < target)
left = m + 1;
else
right = m - 1;
}
return -1;
}
}
def bSearch(arr, left, right, target):
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Time Complexity :
Walkthrough Examples :
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Search in Bitonic Array! | 100 |
|
53:22 | |
Smaller or equal elements | 200 |
|
33:04 | |
WoodCutting Made Easy! | 200 |
|
59:06 | |
Matrix Search | 250 |
|
36:51 | |
Search for a Range | 250 |
|
42:02 | |
Sorted Insert Position | 250 |
|
25:03 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Capacity To Ship Packages Within B Days | 200 |
|
48:21 | |
Matrix Median | 225 |
|
77:24 | |
Square Root of Integer | 275 |
|
40:32 | |
Allocate Books | 350 |
|
68:01 | |
Painter's Partition Problem | 350 |
|
81:38 | |
Red Zone | 400 |
|
45:14 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Implement Power Function | 275 |
|
59:44 | |
Simple Queries | 300 |
|
67:38 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Median of Array | 325 |
|
87:06 | |
Rotated Sorted Array Search | 325 |
|
56:53 |