| Thread Closed |
Prove an Algorithm |
Share Thread | Thread Tools |
| Apr13-05, 05:10 PM | #1 |
|
|
Prove an Algorithm
I have to come up with an algorithm to search a sorted array. Here it is:
Code:
def binarySearch(inputArray, match):
x = -1
start = 0
end = len(inputArray) - 1
while not start == end:
midPt = (start + end) / 2
if match < inputArray[midPt]:
end = midPt - 1
elif match > inputArray[midPt]:
start = midPt + 1
else:
return midPt
if inputArray[start] == match:
x = start
return x
Thanks |
| Apr13-05, 05:35 PM | #2 |
|
Recognitions:
|
Maybe you can use strong induction. Show that it works when len(inputArray) = 1, then show that if it works for all arrays of length k or less, then it works for an array of length k+1. Note that if you go through the while loop once, you come back to the top essentially dealing with an array of 1/2 the length, so it would fit in the category of "arrays of length k or less".
|
| Thread Closed |
| Thread Tools | |
Similar Threads for: Prove an Algorithm
|
||||
| Thread | Forum | Replies | ||
| DFS algorithm | Programming & Comp Sci | 2 | ||
| RSA Algorithm | Computing & Technology | 2 | ||
| Prove the algorithm for LCM | Set Theory, Logic, Probability, Statistics | 7 | ||
| ln(x) algorithm | General Math | 2 | ||