Skip to content

Commit dc37ab5

Browse files
committed
Add BinarySearchTest for both implementations
1 parent 7544a92 commit dc37ab5

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

out/production/src/src/dataStructures/disjointSet/quickFind/quick_find.iml

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/algorithms/searches/binarySearch/BinarySearchTemplated.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BinarySearchTemplated {
3131
*/
3232
// The condition should be changed accordingly
3333
public static boolean condition(int value, int target) {
34-
return value < target;
34+
return value >= target;
3535
}
3636

3737
/**
@@ -47,9 +47,9 @@ public static int search(int[] arr, int target) {
4747
int low = 0;
4848
while (low < high) {
4949
int mid = low + (high - low) / 2; // equivalent to high + low / 2 but reduces cases of integer overflow
50-
if (condition(arr[mid], target)) { // if value < target
50+
if (condition(arr[mid], target)) { // if value >= target
5151
high = mid;
52-
} else { // if value >= target
52+
} else { // if value < target
5353
low = mid + 1;
5454
}
5555
}

src/algorithms/searches/binarySearch/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ This template will work for most binary search problems and will only require th
3333
Simply modify the initialisation of the high and low pointer according to the search space.
3434

3535
### Condition (Requires change)
36-
We assume that when the condition returns true, the current value "fails" and when the condition returns false, the
37-
current value "passes".
36+
We assume that when the condition returns true, the current value "passes" and when the condition returns false, the
37+
current value "fails".
3838

3939
In this template, we want to find the first "pass" in the array.
4040

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
11
package test.algorithms.binarySearchTest;
22

3+
import org.junit.Test;
4+
5+
import src.algorithms.searches.binarySearch.BinarySearchTemplated;
6+
import src.algorithms.searches.binarySearch.BinarySearch;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
313
public class BinarySearchTest {
14+
@Test
15+
public void test_binarySearch() {
16+
// Test 1: even number of elements
17+
int[] firstArray = {1, 5, 10, 12};
18+
int firstResult = BinarySearch.search(firstArray, 1);
19+
20+
// Test 2: odd number of elements
21+
int[] secondArray = {1, 5, 10, 11, 12};
22+
int secondResult = BinarySearch.search(secondArray, 11);
23+
24+
// Test 3: target not in array
25+
int[] thirdArray = {1, 5, 10, 11, 12};
26+
int thirdResult = BinarySearch.search(thirdArray, 3);
27+
28+
assertEquals(0, firstResult);
29+
assertEquals(3, secondResult);
30+
assertEquals(-1, thirdResult);
31+
}
32+
33+
@Test
34+
public void test_binarySearchTemplated() {
35+
// Test 1: even number of elements
36+
int[] firstArray = {1, 5, 10, 12};
37+
int firstResult = BinarySearchTemplated.search(firstArray, 1);
38+
39+
// Test 2: odd number of elements
40+
int[] secondArray = {1, 5, 10, 11, 12};
41+
int secondResult = BinarySearchTemplated.search(secondArray, 11);
42+
43+
// Test 3: target not in array
44+
int[] thirdArray = {1, 5, 10, 11, 12};
45+
int thirdResult = BinarySearchTemplated.search(thirdArray, 3);
46+
47+
assertEquals(0, firstResult);
48+
assertEquals(3, secondResult);
49+
assertEquals(-1, thirdResult);
50+
}
451
}

0 commit comments

Comments
 (0)