Binary Search Algorithm (#2452)
* Binary search * deleting previous files * BinarySearch along with the test case
This commit is contained in:
parent
621d0d2c62
commit
15666e8ed4
@ -1,11 +1,11 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class BinarySearch {
|
public class BinarySearch {
|
||||||
|
|
||||||
public int runBinarySearch() {
|
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
|
||||||
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
|
||||||
int key = 6;
|
|
||||||
|
|
||||||
int low = 0;
|
|
||||||
int high = sortedArray.length - 1;
|
|
||||||
int index = Integer.MAX_VALUE;
|
int index = Integer.MAX_VALUE;
|
||||||
|
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
@ -23,4 +23,31 @@ public class BinarySearch {
|
|||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
|
||||||
|
|
||||||
|
int middle = (low + high) / 2;
|
||||||
|
if (high < low) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key == sortedArray[middle]) {
|
||||||
|
return middle;
|
||||||
|
} else if (key < sortedArray[middle]) {
|
||||||
|
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
|
||||||
|
} else {
|
||||||
|
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
|
||||||
|
int index = Arrays.binarySearch(sortedArray, key);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
|
||||||
|
int index = Collections.binarySearch(sortedList, key);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,41 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
public class BinarySearchTest {
|
public class BinarySearchTest {
|
||||||
|
|
||||||
@Test
|
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
|
int key = 6;
|
||||||
BinarySearch binSearch = new BinarySearch();
|
|
||||||
int expectedIndexForSearchKey = 7;
|
int expectedIndexForSearchKey = 7;
|
||||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
|
int low = 0;
|
||||||
|
int high = sortedArray.length - 1;
|
||||||
|
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user