mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add tests, remove main in JumpSearch (#5669)
This commit is contained in:
@ -2,44 +2,55 @@ package com.thealgorithms.searches;
|
||||
|
||||
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 */
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user