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!
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'Calculation of Tensile Forces in Piston-Type Water-Lifting Devices at Elevated Locations'
Figure 1 Overall Structure Diagram Figure 2: Top view of the piston when it is cylindrical A circular opening is created at a height of 5 meters above the water surface. Inside this opening is a sleeve-type piston with a cross-sectional area of 1 square meter. The piston is pulled to the right at a constant speed. The pulling force is(Figure 2): F = ρshg = 1000 × 1 × 5 × 10 = 50,000 N. Figure 3: Modifying the structure to incorporate a fixed internal piston When I modify the piston...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top