bibary Search Algorithm
bibary Search Algorithm
```python
if arr[mid] == target:
return mid
left = mid + 1
else:
right = mid - 1
# Usage
```
```python
if arr[mid] == target:
return mid
else:
# Usage
```
Find the first occurrence of a target value in a sorted array with duplicates.
```python
def binary_search_first_occurrence(arr, target):
result = -1
if arr[mid] == target:
result = mid
left = mid + 1
else:
right = mid - 1
return result
# Usage
```
```python
def binary_search_sqrt(n):
if n < 2:
return n
left, right = 1, n // 2
if mid * mid == n:
return mid
left = mid + 1
result = mid
else:
right = mid - 1
return result
# Usage
print(binary_search_sqrt(10)) # Output: 3
```
```python
if mid_value == target:
return True
left = mid + 1
else:
right = mid - 1
return False
# Usage
matrix = [
[1, 3, 5],
[7, 9, 11],
```
These examples demonstrate how binary search can be applied to different data structures and problem
types.