Skip to content

Commit 333a1f6

Browse files
HardvanChiefpatwal
authored andcommitted
Add tests, remove main in JumpSearch (TheAlgorithms#5669)
1 parent 39d02c3 commit 333a1f6

File tree

3 files changed

+128
-22
lines changed

3 files changed

+128
-22
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@
10071007
* [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java)
10081008
* [IterativeBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java)
10091009
* [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java)
1010+
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
10101011
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10111012
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10121013
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)

src/main/java/com/thealgorithms/searches/JumpSearch.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,55 @@
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
44

5+
/**
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.
13+
*
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.
17+
*
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.
27+
*/
528
public class JumpSearch implements SearchAlgorithm {
629

7-
public static void main(String[] args) {
8-
JumpSearch jumpSearch = new JumpSearch();
9-
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
10-
for (int i = 0; i < array.length; i++) {
11-
assert jumpSearch.find(array, i) == i;
12-
}
13-
assert jumpSearch.find(array, -1) == -1;
14-
assert jumpSearch.find(array, 11) == -1;
15-
}
16-
1730
/**
18-
* Jump Search algorithm implements
31+
* Jump Search algorithm implementation.
1932
*
20-
* @param array the array contains elements
21-
* @param key to be searched
22-
* @return index of {@code key} if found, otherwise <tt>-1</tt>
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
2336
*/
2437
@Override
2538
public <T extends Comparable<T>> int find(T[] array, T key) {
2639
int length = array.length;
27-
/* length of array */
2840
int blockSize = (int) Math.sqrt(length);
29-
/* block size to be jumped */
3041

3142
int limit = blockSize;
32-
while (key.compareTo(array[limit]) > 0 && limit < array.length - 1) {
33-
limit = Math.min(limit + blockSize, array.length - 1);
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);
3446
}
3547

36-
for (int i = limit - blockSize; i <= limit; i++) {
37-
if (array[i] == key) {
38-
/* execute linear search */
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)) {
3951
return i;
4052
}
4153
}
4254
return -1;
43-
/* not found */
4455
}
4556
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the JumpSearch class.
9+
*/
10+
class JumpSearchTest {
11+
12+
/**
13+
* Test for finding an element present in the array.
14+
*/
15+
@Test
16+
void testJumpSearchFound() {
17+
JumpSearch jumpSearch = new JumpSearch();
18+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
19+
Integer key = 5; // Element to find
20+
assertEquals(5, jumpSearch.find(array, key), "The index of the found element should be 5.");
21+
}
22+
23+
/**
24+
* Test for finding the first element in the array.
25+
*/
26+
@Test
27+
void testJumpSearchFirstElement() {
28+
JumpSearch jumpSearch = new JumpSearch();
29+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
30+
Integer key = 0; // First element
31+
assertEquals(0, jumpSearch.find(array, key), "The index of the first element should be 0.");
32+
}
33+
34+
/**
35+
* Test for finding the last element in the array.
36+
*/
37+
@Test
38+
void testJumpSearchLastElement() {
39+
JumpSearch jumpSearch = new JumpSearch();
40+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
41+
Integer key = 10; // Last element
42+
assertEquals(10, jumpSearch.find(array, key), "The index of the last element should be 10.");
43+
}
44+
45+
/**
46+
* Test for finding an element not present in the array.
47+
*/
48+
@Test
49+
void testJumpSearchNotFound() {
50+
JumpSearch jumpSearch = new JumpSearch();
51+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
52+
Integer key = -1; // Element not in the array
53+
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
54+
}
55+
56+
/**
57+
* Test for finding an element in an empty array.
58+
*/
59+
@Test
60+
void testJumpSearchEmptyArray() {
61+
JumpSearch jumpSearch = new JumpSearch();
62+
Integer[] array = {}; // Empty array
63+
Integer key = 1; // Key not present
64+
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in an empty array.");
65+
}
66+
67+
/**
68+
* Test for finding an element in a large array.
69+
*/
70+
@Test
71+
void testJumpSearchLargeArray() {
72+
JumpSearch jumpSearch = new JumpSearch();
73+
Integer[] array = new Integer[1000];
74+
for (int i = 0; i < array.length; i++) {
75+
array[i] = i * 2; // Fill the array with even numbers
76+
}
77+
Integer key = 256; // Present in the array
78+
assertEquals(128, jumpSearch.find(array, key), "The index of the found element should be 128.");
79+
}
80+
81+
/**
82+
* Test for finding an element in a large array when it is not present.
83+
*/
84+
@Test
85+
void testJumpSearchLargeArrayNotFound() {
86+
JumpSearch jumpSearch = new JumpSearch();
87+
Integer[] array = new Integer[1000];
88+
for (int i = 0; i < array.length; i++) {
89+
array[i] = i * 2; // Fill the array with even numbers
90+
}
91+
Integer key = 999; // Key not present
92+
assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
93+
}
94+
}

0 commit comments

Comments
 (0)