@@ -11,18 +11,18 @@ Image Source: GeeksforGeeks
1111
1212BinarySearch is a more straightforward and intuitive version of the binary search algorithm. In this approach, after the
1313mid-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) when narrowing the search. Similarly, when
15- mid points to index 7 in the second search, the high pointer shifts to index 6 (-1) when narrowing the search. This
16- prevents any possibility of infinite loops.
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.
1718
1819## BinarySearchTemplated
1920
2021BinarySearchTemplated removes the condition that checks if the current mid-value is equal to the target (which helps to
2122end the search the moment the target is found). The template adds a "condition" method which will be modified based on
2223the requirements of the implementation.
2324
24- The narrowing of the search space differs from BinarySearch - only one of the high or low pointers will be adjusted by
25- one unit.
25+ The narrowing of the search space differs from BinarySearch - only the high pointer will be adjusted by one unit.
2626
2727This template will work for most binary search problems and will only require the following changes:
2828- Search space (high and low)
@@ -36,15 +36,34 @@ Simply modify the initialisation of the high and low pointer according to the se
3636We assume that when the condition returns true, the current value "passes" and when the condition returns false, the
3737current value "fails".
3838
39- In this template, we want to find the first "pass" in the array.
40-
41- INSERT IMAGE OF FIRST PASS
39+ Note that in this template, the conditional blocks
40+ ```
41+ if (condition(x)) {
42+ high = mid;
43+ } else {
44+ low = mid + 1;
45+ }
46+ ```
47+ requires elements that "fail" the condition to be on the left of the elements that "pass" the condition, see below, in a
48+ sorted array due to the way the high and low pointers are reassigned.
49+
50+ ![ binary search templated 1 img] ( ../../../../../docs/assets/images/BinarySearchTemplated1.jpeg )
51+
52+ 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?
55+ <details >
56+ <summary > <b >what will happen?</b > </summary >
57+ The array becomes "P P F F F F" and the low and high pointers are now reassigned wrongly.
58+ </details >
4259
4360### Returned Value (Requires change)
4461In the implementation of BinarySearchTemplated, return low was used to find the first "pass".
4562
4663EXPLANATION TBC, STILL THINKING HOW TO PHRASE IT.
4764
65+ ![ binary search templated 1 img] ( ../../../../../docs/assets/images/BinarySearchTemplated2.jpeg )
66+
4867### Search Space Adjustment
4968What should be the search space adjustment? (Why only low = mid + 1)
5069
0 commit comments