@@ -11,18 +11,18 @@ Image Source: GeeksforGeeks
11
11
12
12
BinarySearch is a more straightforward and intuitive version of the binary search algorithm. In this approach, after the
13
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) 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.
17
18
18
19
## BinarySearchTemplated
19
20
20
21
BinarySearchTemplated removes the condition that checks if the current mid-value is equal to the target (which helps to
21
22
end the search the moment the target is found). The template adds a "condition" method which will be modified based on
22
23
the requirements of the implementation.
23
24
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.
26
26
27
27
This template will work for most binary search problems and will only require the following changes:
28
28
- Search space (high and low)
@@ -36,15 +36,34 @@ Simply modify the initialisation of the high and low pointer according to the se
36
36
We assume that when the condition returns true, the current value "passes" and when the condition returns false, the
37
37
current value "fails".
38
38
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 >
42
59
43
60
### Returned Value (Requires change)
44
61
In the implementation of BinarySearchTemplated, return low was used to find the first "pass".
45
62
46
63
EXPLANATION TBC, STILL THINKING HOW TO PHRASE IT.
47
64
65
+ ![ binary search templated 1 img] ( ../../../../../docs/assets/images/BinarySearchTemplated2.jpeg )
66
+
48
67
### Search Space Adjustment
49
68
What should be the search space adjustment? (Why only low = mid + 1)
50
69
0 commit comments