Open ParnaRoyChowdhury777 opened 4 days ago
Hi @ParnaRoyChowdhury777! Thanks for opening this issue. We appreciate your contribution to this open-source project. Your input is valuable and we aim to respond or assign your issue as soon as possible. Thanks again!
@ParnaRoyChowdhury777 We need for this directory advance contents So, if you do define and explain in full details then I assign you.
@ParnaRoyChowdhury777 We need for this directory advance contents So, if you do define and explain in full details then I assign you.
Ok..I am sending the content
@Ajay-Dhangar here is the content ::
Binary search is a classic algorithm for finding an element in a sorted array. It repeatedly divides the search interval in half and compares the middle element of the interval to the target value. If the target value matches the middle element, the search is successful. Otherwise, the search continues in the half of the array that may contain the target value.
Time Complexity:
Efficiency with Large Datasets:
Data Requirements:
Predictability:
Use Cases:
low
and high
, to the beginning and end of the array, respectively.low
is less than or equal to high
:
mid = (low + high) / 2
.high = mid - 1
.low = mid + 1
.-1
).def binary_search_recursive(arr, low, high, target):
if low > high:
return -1 # Target not found
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search_recursive(arr, low, mid - 1, target)
else:
return binary_search_recursive(arr, mid + 1, high, target)
def binary_search_iterative(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1 # Target not found
Recurrence Relation:
T(n)
is the time complexity for an array of size n
, then:
T(n) = T(n/2) + O(1)
.Master's Theorem:
T(n) = T(n/2) + O(1)
fits the form T(n) = aT(n/b) + f(n)
, where a = 1
, b = 2
, and f(n) = O(1)
.T(n) = O(\log n)
Substitution Method:
T(n) = O(\log n)
.T(n) = T(n/2) + O(1) = O(log n) + O(1) = O(log n)
O(1)
since it uses a constant amount of extra space.O(log n)
due to the recursive call stack.Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Target: 7
low = 0, high = 9
mid = (0 + 9) // 2 = 4
Array[mid] = 9
9 > 7, so high = mid - 1 = 3
low = 0, high = 3
mid = (0 + 3) // 2 = 1
Array[mid] = 3
3 < 7, so low = mid + 1 = 2
low = 2, high = 3
mid = (2 + 3) // 2 = 2
Array[mid] = 5
5 < 7, so low = mid + 1 = 3
low = 3, high = 3
mid = (3 + 3) // 2 = 3
Array[mid] = 7
7 == 7, target found at index 3
The array in which searching is to be performed is:
Let x = 4
be the element to be searched.
Set two pointers low and high at the lowest and the highest positions respectively.
Find the middle element mid of the array i.e. arr[(low + high)/2] = 6
.
If x == mid, then return mid. Else, compare the element to be searched with mid.
If x > mid
, compare x with the middle element of the elements on the right side of mid. This is done by setting low to low = mid + 1
.
Else, compare x with the middle element of the elements on the left side of mid. This is done by setting high to high = mid - 1
.
Repeat steps 3 to 6 until low meets high.
x = 4
is found.
Binary search is a fundamental algorithm with wide-ranging applications, especially in scenarios requiring fast data retrieval from sorted datasets.
The last portion images are like this::
Is there an existing issue for this?
Issue Description
I want to add the content over here
Suggested Change
I want to add the content
Rationale
No response
Urgency
High
Acknowledgements