@@ -36,17 +36,22 @@ 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
39
+ the condition method implementation in BinarySearchTemplated from ` value >= target ` to ` value < target ` , what will
40
40
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. We are now looking for the
44
- first "fail" instead of the first "pass" .
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
45
46
- To resolve this issue, multiple changes are required: the pointer assignment bodies have to be swapped, low = mid + 1
47
- needs to be changed to low = mid, high = mid changed to high = mid - 1 AND ceiling division has to be used to calculate
48
- the mid-value. The arrangement of the "pass" elements on the left of the "fail" elements is discouraged as it breaks
49
- away from the convention used in the template.
46
+ We are now looking for the first "fail" instead of the first "pass".
47
+
48
+ To resolve this issue, there are two fixes:
49
+ 1 . Swap the conditional blocks of low - mid + 1 and high = mid.
50
+ ** OR**
51
+ 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
+
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.
50
55
</details >
51
56
52
57
### Returned Value (Requires change)
0 commit comments