Add Jump search algorithm

This commit is contained in:
William Brawner 2018-10-15 18:15:11 -05:00
parent a3bbf1a20f
commit d44f9e33f8
3 changed files with 93 additions and 0 deletions

View file

@ -12,6 +12,7 @@ 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) [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)
[Fibonacci Search](https://en.wikipedia.org/wiki/Fibonacci_search_technique)|[FibonacciSearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/FibonacciSearch.java)|[ParameterizedFibonacciSearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedFibonacciSearchTest.java)|O(log n)|O(1) [Fibonacci Search](https://en.wikipedia.org/wiki/Fibonacci_search_technique)|[FibonacciSearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/FibonacciSearch.java)|[ParameterizedFibonacciSearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedFibonacciSearchTest.java)|O(log n)|O(1)
[Jump Search](https://en.wikipedia.org/wiki/Jump_search)|[FibonacciSearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/JumpSearch.java)|[ParameterizedJumpSearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedJumpSearchTest.java)|O(√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) [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 ### Sorting

View file

@ -0,0 +1,52 @@
package com.wbrawner.algorithms.searching;
/**
* Jump search requires the array to be sorted, and works by "jumping" through the array by segments that are as long
* as the square root of the length of the array until it finds a value greater than the value being searched for, at
* which point it returns to the index of the previous value it had and uses the linear search method to find the
* given value.
*/
class JumpSearch {
/**
* Search the given array for the given element using the Jump 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;
}
if (array.length == 1) {
return array[0] == element ? 0 : -1;
}
final int jumpSize = (int) Math.sqrt(array.length);
int jumpCount = 0;
while (true) {
int index = jumpSize * jumpCount;
if (index >= array.length) {
return -1;
}
if (array[index] == element) {
return index;
}
if (array[index] < element) {
jumpCount++;
continue;
}
if (array[index] > element) {
for (int i = index - jumpSize; i < index; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}
}
}
}

View file

@ -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 ParameterizedJumpSearchTest {
@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,
JumpSearch.search(array, searchFor)
);
}
@Parameterized.Parameters
public static Object[][] getData() {
return SearchData.get();
}
}