Skip to content

Commit b40e447

Browse files
committed
Update Binary Search readme final
1 parent aaf869f commit b40e447

File tree

1 file changed

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

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ Two versions of binary search has been implemented in this repository - BinarySe
1010
Image Source: GeeksforGeeks
1111

1212
BinarySearch is a more straightforward and intuitive version of the binary search algorithm. In this approach, after the
13-
mid-value is calculated, the high and low pointers are adjusted by just one unit. From the above example, after mid
14-
points to index 4 in the first search, the low pointer moves to index 5 (+1 from 4) when narrowing the search.
15-
Similarly, when mid points to index 7 in the second search, the high pointer shifts to index 6 (-1 from 7) when
16-
narrowing the search. This prevents any possibility of infinite loops. During the search, the moment mid-value is equal
17-
to the target value, the search ends prematurely.
13+
mid-value is calculated, the high or low pointer is adjusted by just one unit. From the above example, after mid points
14+
to index 4 in the first search, the low pointer moves to index 5 (+1 from 4) when narrowing the search. Similarly, when
15+
mid points to index 7 in the second search, the high pointer shifts to index 6 (-1 from 7) when narrowing the search.
16+
This prevents any possibility of infinite loops. During the search, the moment mid-value is equal to the target value,
17+
the search ends prematurely. Note that there is no need for a "condition" method as the condition is already captured
18+
in the predicates of the if-else blocks.
1819

1920
## BinarySearchTemplated
2021

@@ -30,7 +31,7 @@ This template will work for most binary search problems and will only require th
3031
- Returned value (low or low - 1)
3132

3233
### Search Space (Requires change)
33-
Simply modify the initialisation of the high and low pointer according to the search space.
34+
Simply modify the initialisation of the high and low pointer according to the [search space](#search-space-adjustment).
3435

3536
### Condition (Requires change)
3637
We assume that when the condition returns true, the current value "passes" and when the condition returns false, the
@@ -50,8 +51,9 @@ sorted array due to the way the high and low pointers are reassigned.
5051
![binary search templated 1 img](../../../../../docs/assets/images/BinarySearchTemplated1.jpeg)
5152

5253
Hence, we will need to implement a condition method that is able to discern between arrays that "pass" and "fail"
53-
accurately and also place them in the correct relative positions i.e. "fail" on the left of "pass". Suppose we change the
54-
condition method implementation in BinarySearchTemplated from `value >= target` to `value <= target`, what will happen?
54+
accurately and also place them in the correct relative positions i.e. "fail" on the left of "pass". Suppose we change
55+
the condition method implementation in BinarySearchTemplated from `value >= target` to `value <= target`, what will
56+
happen?
5557
<details>
5658
<summary> <b>what will happen?</b> </summary>
5759
The array becomes "P P F F F F" and the low and high pointers are now reassigned wrongly.

0 commit comments

Comments
 (0)