@@ -10,11 +10,12 @@ Two versions of binary search has been implemented in this repository - BinarySe
10
10
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
- 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.
18
19
19
20
## BinarySearchTemplated
20
21
@@ -30,7 +31,7 @@ This template will work for most binary search problems and will only require th
30
31
- Returned value (low or low - 1)
31
32
32
33
### 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 ) .
34
35
35
36
### Condition (Requires change)
36
37
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.
50
51
![ binary search templated 1 img] ( ../../../../../docs/assets/images/BinarySearchTemplated1.jpeg )
51
52
52
53
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?
55
57
<details >
56
58
<summary > <b >what will happen?</b > </summary >
57
59
The array becomes "P P F F F F" and the low and high pointers are now reassigned wrongly.
0 commit comments