Skip to content

feat: Add RandomSearch new algorithm with Junit tests #5701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@
* [PerfectBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)
* [QuickSelect](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/QuickSelect.java)
* [RabinKarpAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java)
* [RandomSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RandomSearch.java)
* [RecursiveBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java)
* [RowColumnWiseSorted2dArrayBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java)
* [SaddlebackSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java)
Expand Down Expand Up @@ -1018,6 +1019,7 @@
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
* [RandomSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RandomSearchTest.java)
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
* [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/thealgorithms/searches/RandomSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.thealgorithms.searches;

import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
* A Random Search algorithm that randomly selects an index and checks if the
* value at that index matches the target. It repeats the process until it
* finds the target or checks all elements.
*
* <p>
* Time Complexity: O(n) in the worst case.
* </p>
*
* @author Hardvan
*/
public class RandomSearch implements SearchAlgorithm {

private final Random random = new Random();

/**
* Finds the index of a given element using random search.
*
* @param array Array to search through
* @param key Element to search for
* @return Index of the element if found, -1 otherwise
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
Set<Integer> visitedIndices = new HashSet<>();
int size = array.length;

while (visitedIndices.size() < size) {
int randomIndex = random.nextInt(size);
if (array[randomIndex].compareTo(key) == 0) {
return randomIndex;
}
visitedIndices.add(randomIndex);
}

return -1;
}
}
87 changes: 87 additions & 0 deletions src/test/java/com/thealgorithms/searches/RandomSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.thealgorithms.searches;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class RandomSearchTest {

private RandomSearch randomSearch;

@BeforeEach
void setUp() {
randomSearch = new RandomSearch();
}

@Test
void testElementFound() {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 5;
int index = randomSearch.find(array, key);

assertNotEquals(-1, index, "Element should be found in the array.");
assertEquals(key, array[index], "Element found should match the key.");
}

@Test
void testElementNotFound() {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 11;
int index = randomSearch.find(array, key);

assertEquals(-1, index, "Element not present in the array should return -1.");
}

@Test
void testEmptyArray() {
Integer[] emptyArray = {};
Integer key = 5;
int index = randomSearch.find(emptyArray, key);

assertEquals(-1, index, "Searching in an empty array should return -1.");
}

@Test
void testSingleElementArrayFound() {
Integer[] array = {5};
Integer key = 5;
int index = randomSearch.find(array, key);

assertEquals(0, index, "The key should be found at index 0 in a single-element array.");
}

@Test
void testSingleElementArrayNotFound() {
Integer[] array = {1};
Integer key = 5;
int index = randomSearch.find(array, key);

assertEquals(-1, index, "The key should not be found in a single-element array if it does not match.");
}

@Test
void testDuplicateElementsFound() {
Integer[] array = {1, 2, 3, 4, 5, 5, 5, 7, 8, 9, 10};
Integer key = 5;
int index = randomSearch.find(array, key);

assertNotEquals(-1, index, "The key should be found in the array with duplicates.");
assertEquals(key, array[index], "The key found should be 5.");
}

@Test
void testLargeArray() {
Integer[] largeArray = new Integer[1000];
for (int i = 0; i < largeArray.length; i++) {
largeArray[i] = i + 1; // Fill with values 1 to 1000
}

Integer key = 500;
int index = randomSearch.find(largeArray, key);

assertNotEquals(-1, index, "The key should be found in the large array.");
assertEquals(key, largeArray[index], "The key found should match 500.");
}
}