|
1 | 1 | package com.thealgorithms.searches;
|
2 | 2 |
|
3 |
| -import com.thealgorithms.devutils.searches.SearchAlgorithm; |
4 |
| - |
5 | 3 | /**
|
6 |
| - * An implementation of the Jump Search algorithm. |
7 |
| - * |
8 |
| - * <p> |
9 |
| - * Jump Search is an algorithm for searching sorted arrays. It works by dividing the array |
10 |
| - * into blocks of a fixed size (the block size is typically the square root of the array length) |
11 |
| - * and jumping ahead by this block size to find a range where the target element may be located. |
12 |
| - * Once the range is found, a linear search is performed within that block. |
| 4 | + * Implementation of Jump Search algorithm. |
13 | 5 | *
|
14 |
| - * <p> |
15 |
| - * The Jump Search algorithm is particularly effective for large sorted arrays where the cost of |
16 |
| - * performing a linear search on the entire array would be prohibitive. |
| 6 | + * Time Complexity: O(√n) |
| 7 | + * Space Complexity: O(1) |
17 | 8 | *
|
18 |
| - * <p> |
19 |
| - * Worst-case performance: O(√N)<br> |
20 |
| - * Best-case performance: O(1)<br> |
21 |
| - * Average performance: O(√N)<br> |
22 |
| - * Worst-case space complexity: O(1) |
23 |
| - * |
24 |
| - * <p> |
25 |
| - * This class implements the {@link SearchAlgorithm} interface, providing a generic search method |
26 |
| - * for any comparable type. |
| 9 | + * Reference: https://en.wikipedia.org/wiki/Jump_search |
27 | 10 | */
|
28 |
| -public class JumpSearch implements SearchAlgorithm { |
29 | 11 |
|
30 |
| - /** |
31 |
| - * Jump Search algorithm implementation. |
32 |
| - * |
33 |
| - * @param array the sorted array containing elements |
34 |
| - * @param key the element to be searched |
35 |
| - * @return the index of {@code key} if found, otherwise -1 |
36 |
| - */ |
37 |
| - @Override |
38 |
| - public <T extends Comparable<T>> int find(T[] array, T key) { |
39 |
| - int length = array.length; |
40 |
| - int blockSize = (int) Math.sqrt(length); |
| 12 | +public class JumpSearch { |
| 13 | + |
| 14 | + public static int jumpSearch(int[] arr, int target) { |
| 15 | + int n = arr.length; |
| 16 | + int step = (int) Math.floor(Math.sqrt(n)); |
| 17 | + int prev = 0; |
41 | 18 |
|
42 |
| - int limit = blockSize; |
43 |
| - // Jumping ahead to find the block where the key may be located |
44 |
| - while (limit < length && key.compareTo(array[limit]) > 0) { |
45 |
| - limit = Math.min(limit + blockSize, length - 1); |
| 19 | + while (arr[Math.min(step, n) - 1] < target) { |
| 20 | + prev = step; |
| 21 | + step += Math.floor(Math.sqrt(n)); |
| 22 | + if (prev >= n) |
| 23 | + return -1; |
46 | 24 | }
|
47 | 25 |
|
48 |
| - // Perform linear search within the identified block |
49 |
| - for (int i = limit - blockSize; i <= limit && i < length; i++) { |
50 |
| - if (array[i].equals(key)) { |
| 26 | + for (int i = prev; i < Math.min(step, n); i++) { |
| 27 | + if (arr[i] == target) |
51 | 28 | return i;
|
52 |
| - } |
53 | 29 | }
|
| 30 | + |
54 | 31 | return -1;
|
55 | 32 | }
|
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + int[] arr = { 1, 3, 5, 7, 9, 12, 17, 21, 25 }; |
| 36 | + int target = 12; |
| 37 | + int index = jumpSearch(arr, target); |
| 38 | + System.out.println("Found at index: " + index); |
| 39 | + } |
56 | 40 | }
|
0 commit comments