Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
Expand Down Expand Up @@ -638,6 +639,7 @@
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
Expand Down Expand Up @@ -969,6 +971,7 @@
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Expand Down
55 changes: 33 additions & 22 deletions src/main/java/com/thealgorithms/searches/JumpSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,55 @@

import com.thealgorithms.devutils.searches.SearchAlgorithm;

/**
* An implementation of the Jump Search algorithm.
*
* <p>
* Jump Search is an algorithm for searching sorted arrays. It works by dividing the array
* into blocks of a fixed size (the block size is typically the square root of the array length)
* and jumping ahead by this block size to find a range where the target element may be located.
* Once the range is found, a linear search is performed within that block.
*
* <p>
* The Jump Search algorithm is particularly effective for large sorted arrays where the cost of
* performing a linear search on the entire array would be prohibitive.
*
* <p>
* Worst-case performance: O(√N)<br>
* Best-case performance: O(1)<br>
* Average performance: O(√N)<br>
* Worst-case space complexity: O(1)
*
* <p>
* This class implements the {@link SearchAlgorithm} interface, providing a generic search method
* for any comparable type.
*/
public class JumpSearch implements SearchAlgorithm {

public static void main(String[] args) {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < array.length; i++) {
assert jumpSearch.find(array, i) == i;
}
assert jumpSearch.find(array, -1) == -1;
assert jumpSearch.find(array, 11) == -1;
}

/**
* Jump Search algorithm implements
* Jump Search algorithm implementation.
*
* @param array the array contains elements
* @param key to be searched
* @return index of {@code key} if found, otherwise <tt>-1</tt>
* @param array the sorted array containing elements
* @param key the element to be searched
* @return the index of {@code key} if found, otherwise -1
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
int length = array.length;
/* length of array */
int blockSize = (int) Math.sqrt(length);
/* block size to be jumped */

int limit = blockSize;
while (key.compareTo(array[limit]) > 0 && limit < array.length - 1) {
limit = Math.min(limit + blockSize, array.length - 1);
// Jumping ahead to find the block where the key may be located
while (limit < length && key.compareTo(array[limit]) > 0) {
limit = Math.min(limit + blockSize, length - 1);
}

for (int i = limit - blockSize; i <= limit; i++) {
if (array[i] == key) {
/* execute linear search */
// Perform linear search within the identified block
for (int i = limit - blockSize; i <= limit && i < length; i++) {
if (array[i].equals(key)) {
return i;
}
}
return -1;
/* not found */
}
}
94 changes: 94 additions & 0 deletions src/test/java/com/thealgorithms/searches/JumpSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Unit tests for the JumpSearch class.
*/
class JumpSearchTest {

/**
* Test for finding an element present in the array.
*/
@Test
void testJumpSearchFound() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 5; // Element to find
assertEquals(5, jumpSearch.find(array, key), "The index of the found element should be 5.");
}

/**
* Test for finding the first element in the array.
*/
@Test
void testJumpSearchFirstElement() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 0; // First element
assertEquals(0, jumpSearch.find(array, key), "The index of the first element should be 0.");
}

/**
* Test for finding the last element in the array.
*/
@Test
void testJumpSearchLastElement() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 10; // Last element
assertEquals(10, jumpSearch.find(array, key), "The index of the last element should be 10.");
}

/**
* Test for finding an element not present in the array.
*/
@Test
void testJumpSearchNotFound() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = -1; // Element not in the array
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for finding an element in an empty array.
*/
@Test
void testJumpSearchEmptyArray() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = {}; // Empty array
Integer key = 1; // Key not present
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in an empty array.");
}

/**
* Test for finding an element in a large array.
*/
@Test
void testJumpSearchLargeArray() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = new Integer[1000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2; // Fill the array with even numbers
}
Integer key = 256; // Present in the array
assertEquals(128, jumpSearch.find(array, key), "The index of the found element should be 128.");
}

/**
* Test for finding an element in a large array when it is not present.
*/
@Test
void testJumpSearchLargeArrayNotFound() {
JumpSearch jumpSearch = new JumpSearch();
Integer[] array = new Integer[1000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2; // Fill the array with even numbers
}
Integer key = 999; // Key not present
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
}
}