Proving Algorithm for Searching Sorted Array

  • Thread starter SpatialVacancy
  • Start date
  • Tags
    Algorithm
In summary, the conversation discusses the need to create an algorithm for searching a sorted array and possible approaches for proving its effectiveness. The code provided is in Python and the suggestion of using strong induction is mentioned as a way to prove the algorithm's functionality.
  • #1
SpatialVacancy
24
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
  • #2
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".
 
  • #3
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!
 

FAQ: Proving Algorithm for Searching Sorted Array

1. What is an algorithm for searching a sorted array?

An algorithm for searching a sorted array is a set of logical steps that are used to find a specific element or value in an array that has been sorted in ascending or descending order.

2. Why is it important to have a sorted array when using a search algorithm?

A sorted array allows for more efficient searching because it eliminates the need to look through every element in the array. The order of the elements allows for a more targeted and efficient search.

3. How does a binary search algorithm work?

A binary search algorithm works by dividing the array into two halves and checking the middle element. If the target value is less than the middle element, the algorithm will search the first half of the array. If the target value is greater than the middle element, the algorithm will search the second half. This process is repeated until the target value is found or the array is exhausted.

4. What is the time complexity of a binary search algorithm?

The time complexity of a binary search algorithm is O(log n), where n is the number of elements in the array. This means that the time it takes to search for an element increases at a slower rate as the number of elements in the array increases.

5. What are some advantages of using a binary search algorithm over a linear search algorithm?

Some advantages of using a binary search algorithm over a linear search algorithm include faster search times, especially for larger arrays, as well as the ability to search in a sorted array in logarithmic time. This can be particularly useful for large datasets where efficiency is crucial.

Similar threads

Replies
36
Views
3K
Replies
75
Views
5K
Replies
30
Views
4K
Replies
13
Views
1K
Replies
10
Views
917
Replies
1
Views
1K
Replies
2
Views
1K
Back
Top