Skip to content

Commit 81cee78

Browse files
committed
Modify special case of condition flip and add edge test cases
1 parent 372f93f commit 81cee78

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/main/java/algorithms/binarySearch/binarySearchTemplated/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,22 @@ sorted array due to the way the high and low pointers are reassigned.
3636

3737
Hence, we will need to implement a condition method that is able to discern between arrays that "pass" and "fail"
3838
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
4040
happen?
4141
<details>
4242
<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.
4545

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.
5055
</details>
5156

5257
### Returned Value (Requires change)

src/test/java/algorithms/binarySearch/BinarySearchTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,22 @@ public void test_binarySearchTemplated() {
3636
int[] secondArray = {1, 5, 10, 11, 12};
3737
int secondResult = BinarySearchTemplated.search(secondArray, 11);
3838

39-
// Test 3: target not in array
39+
// Test 3: target not in array but could exist within search space
4040
int[] thirdArray = {1, 5, 10, 11, 12};
4141
int thirdResult = BinarySearchTemplated.search(thirdArray, 3);
4242

43+
// Test 4: target not in array but could exist on the right of search space
44+
int[] fourthArray = {1, 5, 10, 11, 12};
45+
int fourthResult = BinarySearchTemplated.search(thirdArray, 13);
46+
47+
// Test 3: target not in array but could exist on the left of search space
48+
int[] fifthArray = {1, 5, 10, 11, 12};
49+
int fifthResult = BinarySearchTemplated.search(thirdArray, 0);
50+
4351
assertEquals(0, firstResult);
4452
assertEquals(3, secondResult);
4553
assertEquals(-1, thirdResult);
54+
assertEquals(-1, fourthResult);
55+
assertEquals(-1, fifthResult);
4656
}
4757
}

0 commit comments

Comments
 (0)