Add Binary search algorithm
This commit is contained in:
parent
6f1256575d
commit
8b99a576b4
5 changed files with 138 additions and 1 deletions
|
@ -4,12 +4,13 @@ This repository is meant for me to compile a list of examples of different
|
|||
algorithms that I have learned of, and develop a working implementation.
|
||||
These implementations may not be the most efficient, so please do point it
|
||||
out to me if you see something that could be improved upon. The complexity
|
||||
notations were taken from the [Big O Cheat Sheet](http://bigocheatsheet.com/)
|
||||
notations were taken primarily from the [Big O Cheat Sheet](http://bigocheatsheet.com/)
|
||||
|
||||
### Searching
|
||||
|
||||
Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity
|
||||
-----|-----|-----|:-----:|:-----:
|
||||
[Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm)|[BinarySearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/BinarySearch.java)|[ParameterizedBinarySearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedBinarySearchTest.java)|O(log n)|O(1)
|
||||
[Linear Search](https://en.wikipedia.org/wiki/Linear_search)|[LinearSearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/LinearSearch.java)|[ParameterizedLinearSearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedLinearSearchTest.java)|O(n)|O(1)
|
||||
|
||||
### Sorting
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.wbrawner.algorithms.searching;
|
||||
|
||||
/**
|
||||
* Binary search requires the array to be sorted, and works by continuously splitting the array in half until it
|
||||
* finds the element being searched for
|
||||
*/
|
||||
class BinarySearch {
|
||||
/**
|
||||
* Search the given array for the given element using the Binary search algorithm
|
||||
*
|
||||
* @return The index of the array where the element can be found or -1 if it can't be found
|
||||
*/
|
||||
static int search(int[] array, int element) {
|
||||
if (array.length == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return search(array, element, 0, array.length - 1);
|
||||
}
|
||||
|
||||
|
||||
private static int search(int[] array, int element, int begin, int end) {
|
||||
int midPoint = begin + ((end - begin) / 2);
|
||||
|
||||
if (midPoint == begin) {
|
||||
if (array[midPoint] == element) {
|
||||
return midPoint;
|
||||
}
|
||||
|
||||
if (array[end] == element) {
|
||||
return end;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (array[midPoint] > element) {
|
||||
return search(array, element, begin, midPoint);
|
||||
}
|
||||
|
||||
if (array[midPoint] < element) {
|
||||
return search(array, element, midPoint, end);
|
||||
}
|
||||
|
||||
return midPoint;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.wbrawner.algorithms.searching;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class ParameterizedBinarySearchTest {
|
||||
|
||||
@Parameterized.Parameter()
|
||||
public int[] array;
|
||||
|
||||
@Parameterized.Parameter(1)
|
||||
public int searchFor;
|
||||
|
||||
@Parameterized.Parameter(2)
|
||||
public int expectedIndex;
|
||||
|
||||
@Parameterized.Parameter(3)
|
||||
public boolean isSorted;
|
||||
|
||||
@Test
|
||||
public void searchTest() {
|
||||
// The array needs to be sorted for binary search to work.
|
||||
assumeTrue(isSorted);
|
||||
|
||||
assertEquals(
|
||||
expectedIndex,
|
||||
BinarySearch.search(array, searchFor)
|
||||
);
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Object[][] getData() {
|
||||
return SearchData.get();
|
||||
}
|
||||
}
|
|
@ -18,6 +18,10 @@ public class ParameterizedLinearSearchTest {
|
|||
@Parameterized.Parameter(2)
|
||||
public int expectedIndex;
|
||||
|
||||
// Not used for these tests but required to be included
|
||||
@Parameterized.Parameter(3)
|
||||
public boolean isSorted;
|
||||
|
||||
@Test
|
||||
public void searchTest() {
|
||||
assertEquals(
|
||||
|
|
|
@ -17,6 +17,8 @@ class SearchData {
|
|||
2,
|
||||
// Expected index
|
||||
5,
|
||||
// Sorted?
|
||||
false,
|
||||
},
|
||||
// Second data set - sorted array with even number of items
|
||||
new Object[]{
|
||||
|
@ -26,6 +28,8 @@ class SearchData {
|
|||
2,
|
||||
// Expected index
|
||||
1,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
// Third data set - unsorted array with odd number of items
|
||||
new Object[]{
|
||||
|
@ -35,6 +39,8 @@ class SearchData {
|
|||
3,
|
||||
// Expected index
|
||||
8,
|
||||
// Sorted?
|
||||
false,
|
||||
},
|
||||
// Fourth data set - sorted array with odd number of items
|
||||
new Object[]{
|
||||
|
@ -44,6 +50,8 @@ class SearchData {
|
|||
7,
|
||||
// Expected index
|
||||
6,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
// Fifth data set - empty array
|
||||
new Object[]{
|
||||
|
@ -53,6 +61,8 @@ class SearchData {
|
|||
4,
|
||||
// Expected index
|
||||
-1,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
// Sixth data set - a single integer array
|
||||
new Object[]{
|
||||
|
@ -62,6 +72,41 @@ class SearchData {
|
|||
1,
|
||||
// Expected index
|
||||
0,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
// Seventh data set - an integer array with only 2 values
|
||||
new Object[]{
|
||||
// Haystack
|
||||
new int[]{1, 100},
|
||||
// Needle
|
||||
100,
|
||||
// Expected index
|
||||
1,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
// Eighth data set - unsorted array with even number of items
|
||||
new Object[]{
|
||||
// Haystack
|
||||
new int[]{5, 4, 1, 8, 7, 2, 6, 3},
|
||||
// Needle
|
||||
20,
|
||||
// Expected index
|
||||
-1,
|
||||
// Sorted?
|
||||
false,
|
||||
},
|
||||
// Ninth data set - sorted array with even number of items
|
||||
new Object[]{
|
||||
// Haystack
|
||||
new int[]{1, 2, 3, 4, 5, 6, 7, 8},
|
||||
// Needle
|
||||
20,
|
||||
// Expected index
|
||||
-1,
|
||||
// Sorted?
|
||||
true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue