From d44f9e33f8f2ed40dbf58087532e9075890d2e90 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Mon, 15 Oct 2018 18:15:11 -0500 Subject: [PATCH] Add Jump search algorithm --- README.md | 1 + .../algorithms/searching/JumpSearch.java | 52 +++++++++++++++++++ .../ParameterizedJumpSearchTest.java | 40 ++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 searching/src/main/java/com/wbrawner/algorithms/searching/JumpSearch.java create mode 100644 searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedJumpSearchTest.java diff --git a/README.md b/README.md index b310512..6de40d4 100644 --- a/README.md +++ b/README.md @@ -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) [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) ### Sorting diff --git a/searching/src/main/java/com/wbrawner/algorithms/searching/JumpSearch.java b/searching/src/main/java/com/wbrawner/algorithms/searching/JumpSearch.java new file mode 100644 index 0000000..dddd2e6 --- /dev/null +++ b/searching/src/main/java/com/wbrawner/algorithms/searching/JumpSearch.java @@ -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; + } + } + } +} diff --git a/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedJumpSearchTest.java b/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedJumpSearchTest.java new file mode 100644 index 0000000..d043941 --- /dev/null +++ b/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedJumpSearchTest.java @@ -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(); + } +} \ No newline at end of file