Skip to content

Commit 9f26f2f

Browse files
author
Deepak Malik
committed
Binary Search
1 parent 1a0cc1e commit 9f26f2f

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/com/deepak/algorithms/Searching/BinarySearch.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@
55
package com.deepak.algorithms.Searching;
66

77
/**
8-
* Class for BinarySearch implementation
9-
* @author Deepak Malik
8+
* Binary search implementation
9+
*
10+
* @author Deepak
1011
*/
1112
public class BinarySearch {
1213

13-
/**
14-
* Main method to start the flow of program
15-
* @param args
16-
*/
17-
public static void main(String[] args) {
18-
int[] sortedValues = {7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93};
19-
int valueToBeSearched = 84;
20-
System.out.println("******************* BINARY SEARCH *******************");
21-
performBinarySearch(sortedValues, valueToBeSearched);
22-
}
23-
2414
/**
2515
* <p>Binary Search
2616
* Few points to note here,
@@ -49,27 +39,23 @@ public static void main(String[] args) {
4939
* => On an average, search term is anywhere in the list
5040
* </p>
5141
*/
52-
private static void performBinarySearch(int[] iListOfValues, int iValueToBeSearched) {
42+
public static int performBinarySearch(int[] listOfValues, int targetValue) {
5343
int index = -1;
5444
int low = 0;
5545
int mid;
56-
int high = iListOfValues.length - 1;
46+
int high = listOfValues.length - 1;
5747
while (high >= low) {
5848
mid = (high + low)/2;
59-
if (iValueToBeSearched < iListOfValues[mid]) {
49+
if (targetValue < listOfValues[mid]) {
6050
high = mid - 1;
61-
} else if (iValueToBeSearched > iListOfValues[mid]) {
51+
} else if (targetValue > listOfValues[mid]) {
6252
low = mid + 1;
6353
} else {
6454
index = mid;
6555
break;
6656
}
6757
}
68-
if (index != -1) {
69-
System.out.println("Element found in the list at position = " + index);
70-
} else {
71-
System.out.println("Element not found in the list");
72-
}
58+
return index;
7359
}
7460

7561
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Algorithms-in-Java
3+
* BinarySearch_Test.java
4+
*/
5+
package com.deepak.algorithms.Searching;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for binary search
12+
*
13+
* @author Deepak
14+
*/
15+
public class BinarySearch_Test {
16+
17+
/**
18+
* Test cases for Binary Search
19+
*/
20+
@Test
21+
public void testBinarySearch() {
22+
int[] listOfValues = {7, 10, 17, 20, 23, 24, 35, 41, 42, 55, 59, 66, 75, 80, 83, 93, 94, 100};
23+
Assert.assertTrue(BinarySearch.performBinarySearch(listOfValues, 55) == 9);
24+
Assert.assertTrue(BinarySearch.performBinarySearch(listOfValues, 0) == -1);
25+
Assert.assertTrue(BinarySearch.performBinarySearch(listOfValues, 100) == listOfValues.length - 1);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)