Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 90a4c36

Browse filesBrowse files
committed
Added binary search and test
1 parent 5b76fca commit 90a4c36
Copy full SHA for 90a4c36

File tree

Expand file treeCollapse file tree

3 files changed

+75
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+75
-0
lines changed
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Java.iml
2+
.idea/*
23
out/
4+
Downloads.iml
Collapse file
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package src.main.com.java.search;
2+
3+
/**
4+
* Binary search is an algorithm which finds the position of a target value within a sorted array
5+
*
6+
* Worst-case performance O(log n)
7+
* Best-case performance O(1)
8+
* Average performance O(log n)
9+
* Worst-case space complexity O(1)
10+
*/
11+
public class BinarySearch {
12+
13+
/**
14+
* @param array is an array where the element should be found
15+
* @param key is an element which should be found
16+
* @param <T> is any comparable type
17+
* @return index of the element
18+
*/
19+
public <T extends Comparable<T>> int findIndex(T array[], T key) {
20+
return search(array, key, 0, array.length-1);
21+
}
22+
23+
/**
24+
* @param array The array to make the binary search
25+
* @param key The number you are looking for
26+
* @param left The lower bound
27+
* @param right The upper bound
28+
* @return the location of the key
29+
**/
30+
private <T extends Comparable<T>> int search(T array[], T key, int left, int right){
31+
if (left > right) {
32+
return -1; // Key not found
33+
}
34+
35+
// Find median
36+
int median = (left + right)/2;
37+
int comp = key.compareTo(array[median]);
38+
39+
if (comp < 0) {
40+
return search(array, key, left, median - 1);
41+
}
42+
43+
if (comp > 0) {
44+
return search(array, key, median + 1, right);
45+
}
46+
47+
return median;
48+
}
49+
}
Collapse file
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package src.test.com.java.search;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.com.java.search.BinarySearch;
6+
7+
public class BinarySearchTest {
8+
9+
@Test
10+
public void testBinarySearch() {
11+
BinarySearch binarySearch = new BinarySearch();
12+
13+
Integer[] arr1 = {1,2,3,4,5};
14+
Assert.assertEquals("Incorrect index", 2, binarySearch.findIndex(arr1,3));
15+
Assert.assertEquals("Incorrect index", 0, binarySearch.findIndex(arr1,1));
16+
Assert.assertEquals("Incorrect index", -1, binarySearch.findIndex(arr1,8));
17+
Assert.assertEquals("Incorrect index", -1, binarySearch.findIndex(arr1,-2));
18+
19+
String[] arr2 = {"A", "B", "C", "D"};
20+
Assert.assertEquals("Incorrect index", 2, binarySearch.findIndex(arr2,"C"));
21+
Assert.assertEquals("Incorrect index", 1, binarySearch.findIndex(arr2,"B"));
22+
Assert.assertEquals("Incorrect index", -1, binarySearch.findIndex(arr2,"F"));
23+
}
24+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.