Skip to content

Commit 23e93ed

Browse files
committed
feat: Add RandomSearch new algorithm with Junit tests
1 parent 90d20b3 commit 23e93ed

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.searches;
2+
3+
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
import java.util.HashSet;
5+
import java.util.Random;
6+
import java.util.Set;
7+
8+
/**
9+
* A Random Search algorithm that randomly selects an index and checks if the
10+
* value at that index matches the target. It repeats the process until it
11+
* finds the target or checks all elements.
12+
*
13+
* <p>
14+
* Time Complexity: O(n) in the worst case.
15+
* </p>
16+
*
17+
* @author Hardvan
18+
*/
19+
public class RandomSearch implements SearchAlgorithm {
20+
21+
private final Random random = new Random();
22+
23+
/**
24+
* Finds the index of a given element using random search.
25+
*
26+
* @param array Array to search through
27+
* @param key Element to search for
28+
* @return Index of the element if found, -1 otherwise
29+
*/
30+
@Override
31+
public <T extends Comparable<T>> int find(T[] array, T key) {
32+
Set<Integer> visitedIndices = new HashSet<>();
33+
int size = array.length;
34+
35+
while (visitedIndices.size() < size) {
36+
int randomIndex = random.nextInt(size);
37+
if (array[randomIndex].compareTo(key) == 0) {
38+
return randomIndex;
39+
}
40+
visitedIndices.add(randomIndex);
41+
}
42+
43+
return -1;
44+
}
45+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
class RandomSearchTest {
10+
11+
private RandomSearch randomSearch;
12+
13+
@BeforeEach
14+
void setUp() {
15+
randomSearch = new RandomSearch();
16+
}
17+
18+
@Test
19+
void testElementFound() {
20+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
21+
Integer key = 5;
22+
int index = randomSearch.find(array, key);
23+
24+
assertNotEquals(-1, index, "Element should be found in the array.");
25+
assertEquals(key, array[index], "Element found should match the key.");
26+
}
27+
28+
@Test
29+
void testElementNotFound() {
30+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
31+
Integer key = 11;
32+
int index = randomSearch.find(array, key);
33+
34+
assertEquals(-1, index, "Element not present in the array should return -1.");
35+
}
36+
37+
@Test
38+
void testEmptyArray() {
39+
Integer[] emptyArray = {};
40+
Integer key = 5;
41+
int index = randomSearch.find(emptyArray, key);
42+
43+
assertEquals(-1, index, "Searching in an empty array should return -1.");
44+
}
45+
46+
@Test
47+
void testSingleElementArrayFound() {
48+
Integer[] array = {5};
49+
Integer key = 5;
50+
int index = randomSearch.find(array, key);
51+
52+
assertEquals(0, index, "The key should be found at index 0 in a single-element array.");
53+
}
54+
55+
@Test
56+
void testSingleElementArrayNotFound() {
57+
Integer[] array = {1};
58+
Integer key = 5;
59+
int index = randomSearch.find(array, key);
60+
61+
assertEquals(-1, index, "The key should not be found in a single-element array if it does not match.");
62+
}
63+
64+
@Test
65+
void testDuplicateElementsFound() {
66+
Integer[] array = {1, 2, 3, 4, 5, 5, 5, 7, 8, 9, 10};
67+
Integer key = 5;
68+
int index = randomSearch.find(array, key);
69+
70+
assertNotEquals(-1, index, "The key should be found in the array with duplicates.");
71+
assertEquals(key, array[index], "The key found should be 5.");
72+
}
73+
74+
@Test
75+
void testLargeArray() {
76+
Integer[] largeArray = new Integer[1000];
77+
for (int i = 0; i < largeArray.length; i++) {
78+
largeArray[i] = i + 1; // Fill with values 1 to 1000
79+
}
80+
81+
Integer key = 500;
82+
int index = randomSearch.find(largeArray, key);
83+
84+
assertNotEquals(-1, index, "The key should be found in the large array.");
85+
assertEquals(key, largeArray[index], "The key found should match 500.");
86+
}
87+
}

0 commit comments

Comments
 (0)