Proving Algorithm for Searching Sorted Array

  • Thread starter Thread starter SpatialVacancy
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread Summary
The discussion centers on a Python implementation of a binary search algorithm for finding an element in a sorted array. To prove its correctness, participants suggest using strong induction, demonstrating that the algorithm works for arrays of increasing lengths. Additionally, a proof by contradiction is proposed, showing that if the algorithm fails to find an element, it leads to a logical inconsistency regarding the element's presence in the array. The algorithm's structure ensures that it correctly updates the search indices, reinforcing its reliability. Overall, the conversation provides insights into validating the effectiveness of the binary search algorithm.
SpatialVacancy
Messages
24
Reaction score
0
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

The code is Python, but I figure anyone can read it without much explination. I have to prove that this algorithm works. I don't know how to do this! Any help would be appreciated!

Thanks
 
Physics news on Phys.org
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".
 
for sharing your algorithm for searching a sorted array. It looks like you have implemented a binary search algorithm, which is a very efficient way to search for an element in a sorted array.

To prove that your algorithm works, we can use a proof by contradiction. This means we will assume that your algorithm does not work and then show that this leads to a contradiction, thus proving that your algorithm must work.

Assume that your algorithm does not work. This means that there exists an input array and a target element that your algorithm fails to find. Let's call this input array A and the target element T.

Since your algorithm did not find T, this means that T is either not present in the array A or your algorithm did not correctly identify its index in the array.

If T is not present in the array A, then this means that your algorithm correctly identified that T is not present in the array. This is because your algorithm checks if the target element is less than or greater than the middle element, and if it is neither, then it must be equal to the middle element. Since T is not present in the array, it must not be equal to the middle element, and your algorithm correctly identifies this.

Now, if T is present in the array A, but your algorithm did not correctly identify its index, then this means that your algorithm either returned -1 (denoting that the element was not found) or it returned an incorrect index.

If your algorithm returned -1, then this contradicts our assumption that T is present in the array A.

If your algorithm returned an incorrect index, then this means that your algorithm did not correctly update the start and end indices in the while loop. However, this contradicts the fact that your algorithm is based on a binary search, which is known to correctly update the start and end indices.

Thus, we have shown that our assumption that your algorithm does not work leads to a contradiction. Hence, your algorithm must work, and we have proven it by contradiction.

I hope this helps you understand how to prove the correctness of your algorithm. Keep up the good work!
 
TL;DR Summary: I came across this question from a Sri Lankan A-level textbook. Question - An ice cube with a length of 10 cm is immersed in water at 0 °C. An observer observes the ice cube from the water, and it seems to be 7.75 cm long. If the refractive index of water is 4/3, find the height of the ice cube immersed in the water. I could not understand how the apparent height of the ice cube in the water depends on the height of the ice cube immersed in the water. Does anyone have an...
Kindly see the attached pdf. My attempt to solve it, is in it. I'm wondering if my solution is right. My idea is this: At any point of time, the ball may be assumed to be at an incline which is at an angle of θ(kindly see both the pics in the pdf file). The value of θ will continuously change and so will the value of friction. I'm not able to figure out, why my solution is wrong, if it is wrong .
Back
Top