Skip to content

Commit 37ef1e4

Browse files
committed
Update README for BinarySearchTemplated
1 parent 81cee78 commit 37ef1e4

File tree

1 file changed

+8
-9
lines changed
  • src/main/java/algorithms/binarySearch/binarySearchTemplated

1 file changed

+8
-9
lines changed

src/main/java/algorithms/binarySearch/binarySearchTemplated/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,21 @@ sorted array due to the way the high and low pointers are reassigned.
3636

3737
Hence, we will need to implement a condition method that is able to discern between arrays that "pass" and "fail"
3838
accurately and also place them in the correct relative positions i.e. "fail" on the left of "pass". Suppose we change
39-
the condition method implementation in BinarySearchTemplated from `value >= target` to `value < target`, what will
40-
happen?
39+
the condition method implementation by inverting the inputs that "pass" the condition, i.e, inputs that "fail" now
40+
"pass" and vice-versa, what will happen?
4141
<details>
4242
<summary> <b>what will happen?</b> </summary>
43-
The array becomes "P P F F F F" and the low and high pointers are now reassigned wrongly - the loop invariant is broken
44-
as the search space is narrowed down in the wrong direction.
45-
46-
We are now looking for the first "fail" instead of the first "pass".
43+
The array becomes "P P F F F F" and our current template will not work as expected. This is because the low and high
44+
pointers are now reassigned wrongly - the loop invariant is broken as the search space is narrowed down in the wrong
45+
direction.
4746

4847
To resolve this issue, there are two fixes:
49-
1. Swap the conditional blocks of low - mid + 1 and high = mid.
48+
1. Swap the conditional blocks of low = mid + 1 and high = mid.
5049
**OR**
5150
2. Simply add a "not" in front of the condition, converting "P P F F F F" back to "F F P P P P".
5251

53-
Note that some conditions may be easier to define with "pass" elements being on the left of "fail" elements, hence, it
54-
is important to adjust the code with the above fixes accordingly.
52+
Note that some conditions may be easier to define with "pass" elements being on the left of "fail" elements, i.e.
53+
"P P F F F F", hence, it is important to adjust the code with the above fixes accordingly.
5554
</details>
5655

5756
### Returned Value (Requires change)

0 commit comments

Comments
 (0)