What are the pitfalls in implementing binary search?

In summary, the Pascal code has a flaw where the algorithm will return false even if the key is not found in the array.
  • #1
dalcde
166
0
On the website http://stackoverflow.com/questions/504335/what-are-the-pitfalls-in-implementing-binary-search, it states that the Pascal code (note that in Pascal, the array indexing starts with 1)
Code:
PROCEDURE BinarySearch (A         : anArray,
                        Size      : anArraySize,
                        Key       : INTEGER,
                        VAR Found : BOOLEAN;
                        VAR Index : anArrayIndex);
Var Low, High : anArrayIndex;
BEGIN         
   LOW := 1;
   High := Size;

   REPEAT
      Index := (Low + High) DIV 2;
      If Key < A[Index]
         THEN High := Index - 1
         ELSE Low  := Index + 1
   UNTIL (Low > High) OR (Key = A[Index]);

   FOUND := (Low <= High)
END;
has the pitfall that when the final iteration starts with Low=High, then it will return
Code:
FOUND=true
even if the Key is not found in the array.

I've tried with the following example and didn't find a mistake:
A=[3]
Key=2
Low=1
High=1
Index=1
Since 2<3, Key < A[index] and
High=Index-1=0
Since Low>High, the loop is terminated
Low<=High isn't true so FOUND is set to false, which is a correct answer.
 
Technology news on Phys.org
  • #2
I have noticed that when the key is 3, we have
Key=3
Low=1
High=1
Index=1
Key<A[1] is false, hence
Low =Index+1=2
Since Low>High, loop ends

FOUND=(Low<=High)=False

This is an example in which the algorithm fails. Am I correct?
 
  • #3
There's a lot wrong with this and it's clear either (a) an engineer/scientist/mathematician wrote this without a clue or (b) this is an exercise devised by a computer scientist. Either way...

Trivially, the pitfall they mention does not hold and the problem you found does hold. If the loop body ever executes with low = high, the condition will fail (since either low is incremented or high is decremented) and high < low holds. So whenever the loop gets to the point where low = high, the algorithm will return false, regardless of whether the element at the position index = low = high is the target or not.

There are two methods to fix this. Note that the problem only exists where there is an element in the array that matches the target and we happen to test it at the very last possible iteration of the loop. So...
Method #1: Change "ELSE LOW := INDEX + 1" to "IF key > A[index] THEN LOW := INDEX + 1".
Method #2: Change "FOUND = (LOW <= HIGH)" to "FOUND = (key = A[index])".

Let me know if I goofed.
 

Related to What are the pitfalls in implementing binary search?

1. What is a binary search algorithm?

A binary search algorithm is a commonly used method for finding a specific value within a sorted list or array of data. It works by repeatedly dividing the search space in half until the desired value is found or determined to not be present. This makes it a very efficient search algorithm, particularly for large data sets.

2. How does a binary search algorithm work?

To perform a binary search, the list of data must first be sorted in either ascending or descending order. The algorithm then begins by comparing the target value to the middle element of the list. If they are equal, the search is complete. If the target value is smaller than the middle element, the search continues in the lower half of the list. If the target value is larger, the search continues in the upper half. This process is repeated until the target value is found or determined to not be present.

3. 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 list. This means that as the size of the list increases, the time taken to complete the search will increase at a much slower rate compared to other search algorithms such as linear search, which has a time complexity of O(n). This makes binary search a very efficient algorithm for searching large data sets.

4. What are the advantages of using a binary search algorithm?

One of the main advantages of using a binary search algorithm is its efficiency. As mentioned before, its time complexity is O(log n), which makes it much faster than other search algorithms for large data sets. Additionally, binary search only requires a sorted list of data, whereas other search algorithms may require the data to be in a specific structure or format. This makes it a more versatile algorithm that can be applied to a variety of problems.

5. Are there any limitations to using a binary search algorithm?

One limitation of using a binary search algorithm is that the data must be sorted beforehand. If the data is not already sorted, the algorithm will not work and the data must first be sorted using another method. Additionally, binary search may not be the best choice for data sets that frequently change, as the list would need to be re-sorted each time. Finally, binary search is only effective for finding a single value and cannot be used for more complex searches such as finding the highest or lowest value in a list.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
5K
  • Programming and Computer Science
Replies
2
Views
722
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
191
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top