Skip to content

Commit 15666e8

Browse files
techPacketspivovarit
authored andcommitted
Binary Search Algorithm (eugenp#2452)
* Binary search * deleting previous files * BinarySearch along with the test case
1 parent 621d0d2 commit 15666e8

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import java.util.Arrays;
2+
import java.util.Collections;
3+
import java.util.List;
4+
15
public class BinarySearch {
26

3-
public int runBinarySearch() {
4-
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
5-
int key = 6;
7+
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
68

7-
int low = 0;
8-
int high = sortedArray.length - 1;
99
int index = Integer.MAX_VALUE;
1010

1111
while (low <= high) {
@@ -23,4 +23,31 @@ public int runBinarySearch() {
2323
}
2424
return index;
2525
}
26+
27+
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
28+
29+
int middle = (low + high) / 2;
30+
if (high < low) {
31+
return -1;
32+
}
33+
34+
if (key == sortedArray[middle]) {
35+
return middle;
36+
} else if (key < sortedArray[middle]) {
37+
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
38+
} else {
39+
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
40+
}
41+
}
42+
43+
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
44+
int index = Arrays.binarySearch(sortedArray, key);
45+
return index;
46+
}
47+
48+
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
49+
int index = Collections.binarySearch(sortedList, key);
50+
return index;
51+
}
52+
2653
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
14
import org.junit.Assert;
25
import org.junit.Test;
36

47

58
public class BinarySearchTest {
69

10+
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
11+
int key = 6;
12+
int expectedIndexForSearchKey = 7;
13+
int low = 0;
14+
int high = sortedArray.length - 1;
15+
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
16+
717
@Test
8-
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
18+
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
919
BinarySearch binSearch = new BinarySearch();
10-
int expectedIndexForSearchKey = 7;
11-
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
20+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
1221
}
1322

23+
@Test
24+
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
25+
BinarySearch binSearch = new BinarySearch();
26+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
27+
}
28+
29+
@Test
30+
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
31+
BinarySearch binSearch = new BinarySearch();
32+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
33+
}
34+
35+
@Test
36+
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
37+
BinarySearch binSearch = new BinarySearch();
38+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
39+
}
40+
1441
}

0 commit comments

Comments
 (0)