Skip to content

Add tests, remove main in LinearSearchThread #5671

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 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Expand Down
83 changes: 43 additions & 40 deletions src/main/java/com/thealgorithms/searches/LinearSearchThread.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
package com.thealgorithms.searches;

import java.util.Scanner;

/**
* LinearSearchThread is a multithreaded implementation of the linear search algorithm.
* It creates multiple threads to search for a specific number in an array.
*
* <p>
* The class generates an array of random integers, prompts the user to enter a number to search for,
* and divides the array into four segments, each handled by a separate thread.
* The threads run concurrently and search for the specified number within their designated segments.
* Finally, it consolidates the results to determine if the number was found.
* </p>
*
* <p>
* Example usage:
* 1. The program will output the generated array.
* 2. The user will be prompted to input a number to search for.
* 3. The program will display whether the number was found in the array.
* </p>
*/
public final class LinearSearchThread {
private LinearSearchThread() {
}

public static void main(String[] args) {
int[] list = new int[200];
for (int j = 0; j < list.length; j++) {
list[j] = (int) (Math.random() * 100);
}
for (int y : list) {
System.out.print(y + " ");
}
System.out.println();
System.out.print("Enter number to search for: ");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
Searcher t = new Searcher(list, 0, 50, x);
Searcher t1 = new Searcher(list, 50, 100, x);
Searcher t2 = new Searcher(list, 100, 150, x);
Searcher t3 = new Searcher(list, 150, 200, x);
t.start();
t1.start();
t2.start();
t3.start();
try {
t.join();
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
}
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
System.out.println("Found = " + found);
in.close();
}
}

/**
* The Searcher class extends Thread and is responsible for searching for a specific
* number in a segment of an array.
*/
class Searcher extends Thread {
private final int[] arr; // The array to search in
private final int left; // Starting index of the segment
private final int right; // Ending index of the segment
private final int x; // The number to search for
private boolean found; // Result flag

private final int[] arr;
private final int left;
private final int right;
private final int x;
private boolean found;

/**
* Constructor to initialize the Searcher.
*
* @param arr The array to search in
* @param left The starting index of the segment
* @param right The ending index of the segment
* @param x The number to search for
*/
Searcher(int[] arr, int left, int right, int x) {
this.arr = arr;
this.left = left;
this.right = right;
this.x = x;
}

/**
* The run method for the thread, performing the linear search in its segment.
*/
@Override
public void run() {
int k = left;
Expand All @@ -65,6 +63,11 @@ public void run() {
}
}

/**
* Returns whether the number was found in the segment.
*
* @return true if the number was found, false otherwise
*/
boolean getResult() {
return found;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class LinearSearchThreadTest {

/**
* Test for finding an element that is present in the array.
*/
@Test
void testSearcherFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, array.length, 5);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The element 5 should be found in the array.");
}

/**
* Test for finding an element that is not present in the array.
*/
@Test
void testSearcherNotFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, array.length, 11);
searcher.start();
searcher.join();
assertFalse(searcher.getResult(), "The element 11 should not be found in the array.");
}

/**
* Test for searching a segment of the array.
*/
@Test
void testSearcherSegmentFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, 5, 3);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The element 3 should be found in the segment.");
}

/**
* Test for searching an empty array segment.
*/
@Test
void testSearcherEmptySegment() throws InterruptedException {
int[] array = {};
Searcher searcher = new Searcher(array, 0, 0, 1); // Empty array
searcher.start();
searcher.join();
assertFalse(searcher.getResult(), "The element should not be found in an empty segment.");
}

/**
* Test for searching with random numbers.
*/
@Test
void testSearcherRandomNumbers() throws InterruptedException {
int size = 200;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = (int) (Math.random() * 100);
}
int target = array[(int) (Math.random() * size)]; // Randomly select a target that is present
Searcher searcher = new Searcher(array, 0, size, target);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The randomly selected target should be found in the array.");
}
}