What are the pitfalls in implementing binary search?

Click For Summary
The discussion centers on a binary search implementation in Pascal that has a critical flaw. The code incorrectly sets the FOUND variable to true when the final iteration starts with Low equal to High, even if the key is not present in the array. An example illustrates that when the key is less than or equal to the element at the index, the algorithm fails to identify the absence of the key correctly. The conversation highlights that the pitfall mentioned in the original source does not apply, while the actual issue does exist. Two potential solutions are proposed: modifying the condition for updating Low or changing how FOUND is determined to ensure accurate results. The discussion concludes by inviting feedback on the proposed corrections.
dalcde
Messages
164
Reaction score
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
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?
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
1K
Replies
9
Views
7K
Replies
235
Views
14K