@@ -36,22 +36,21 @@ sorted array due to the way the high and low pointers are reassigned.
36
36
37
37
Hence, we will need to implement a condition method that is able to discern between arrays that "pass" and "fail"
38
38
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?
41
41
<details >
42
42
<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.
47
46
48
47
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.
50
49
** OR**
51
50
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".
52
51
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.
55
54
</details >
56
55
57
56
### Returned Value (Requires change)
0 commit comments