6
Prove Binary Search Correctness
medium25 pts
Statement#
Consider the following binary search algorithm:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Prove the correctness of this algorithm using the loop invariant technique. Your proof should include:
- Initialization: The invariant holds before the first iteration.
- Maintenance: If the invariant holds before an iteration, it holds after.
- Termination: The loop terminates and the invariant implies correctness.
Required Topics#
- Loop invariants
- Algorithm correctness proofs
- Binary search properties
Solution#
Solution coming soon.
Hints (4)
Topics Needed
binary-searchloop-invariantsalgorithm-correctness
Prerequisites
- basic-algorithms
- mathematical-induction
Statistics
0
Total Attempts
0%
Success Rate
0%
First Try Success
0%
Completion Rate
6
Prove Binary Search Correctness
medium25 pts
Statement#
Consider the following binary search algorithm:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Prove the correctness of this algorithm using the loop invariant technique. Your proof should include:
- Initialization: The invariant holds before the first iteration.
- Maintenance: If the invariant holds before an iteration, it holds after.
- Termination: The loop terminates and the invariant implies correctness.
Required Topics#
- Loop invariants
- Algorithm correctness proofs
- Binary search properties
Solution#
Solution coming soon.
Hints (4)
Topics Needed
binary-searchloop-invariantsalgorithm-correctness
Prerequisites
- basic-algorithms
- mathematical-induction
Statistics
0
Total Attempts
0%
Success Rate
0%
First Try Success
0%
Completion Rate