Skip to content

Commit df0d07c

Browse files
HardvanChiefpatwal
authored andcommitted
Add tests, remove main in LinearSearchThread (TheAlgorithms#5671)
1 parent b746e6d commit df0d07c

File tree

3 files changed

+118
-40
lines changed

3 files changed

+118
-40
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@
10101010
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
10111011
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10121012
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
1013+
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
10131014
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
10141015
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10151016
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)

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

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Scanner;
4-
3+
/**
4+
* LinearSearchThread is a multithreaded implementation of the linear search algorithm.
5+
* It creates multiple threads to search for a specific number in an array.
6+
*
7+
* <p>
8+
* The class generates an array of random integers, prompts the user to enter a number to search for,
9+
* and divides the array into four segments, each handled by a separate thread.
10+
* The threads run concurrently and search for the specified number within their designated segments.
11+
* Finally, it consolidates the results to determine if the number was found.
12+
* </p>
13+
*
14+
* <p>
15+
* Example usage:
16+
* 1. The program will output the generated array.
17+
* 2. The user will be prompted to input a number to search for.
18+
* 3. The program will display whether the number was found in the array.
19+
* </p>
20+
*/
521
public final class LinearSearchThread {
622
private LinearSearchThread() {
723
}
8-
9-
public static void main(String[] args) {
10-
int[] list = new int[200];
11-
for (int j = 0; j < list.length; j++) {
12-
list[j] = (int) (Math.random() * 100);
13-
}
14-
for (int y : list) {
15-
System.out.print(y + " ");
16-
}
17-
System.out.println();
18-
System.out.print("Enter number to search for: ");
19-
Scanner in = new Scanner(System.in);
20-
int x = in.nextInt();
21-
Searcher t = new Searcher(list, 0, 50, x);
22-
Searcher t1 = new Searcher(list, 50, 100, x);
23-
Searcher t2 = new Searcher(list, 100, 150, x);
24-
Searcher t3 = new Searcher(list, 150, 200, x);
25-
t.start();
26-
t1.start();
27-
t2.start();
28-
t3.start();
29-
try {
30-
t.join();
31-
t1.join();
32-
t2.join();
33-
t3.join();
34-
} catch (InterruptedException e) {
35-
}
36-
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
37-
System.out.println("Found = " + found);
38-
in.close();
39-
}
4024
}
4125

26+
/**
27+
* The Searcher class extends Thread and is responsible for searching for a specific
28+
* number in a segment of an array.
29+
*/
4230
class Searcher extends Thread {
31+
private final int[] arr; // The array to search in
32+
private final int left; // Starting index of the segment
33+
private final int right; // Ending index of the segment
34+
private final int x; // The number to search for
35+
private boolean found; // Result flag
4336

44-
private final int[] arr;
45-
private final int left;
46-
private final int right;
47-
private final int x;
48-
private boolean found;
49-
37+
/**
38+
* Constructor to initialize the Searcher.
39+
*
40+
* @param arr The array to search in
41+
* @param left The starting index of the segment
42+
* @param right The ending index of the segment
43+
* @param x The number to search for
44+
*/
5045
Searcher(int[] arr, int left, int right, int x) {
5146
this.arr = arr;
5247
this.left = left;
5348
this.right = right;
5449
this.x = x;
5550
}
5651

52+
/**
53+
* The run method for the thread, performing the linear search in its segment.
54+
*/
5755
@Override
5856
public void run() {
5957
int k = left;
@@ -65,6 +63,11 @@ public void run() {
6563
}
6664
}
6765

66+
/**
67+
* Returns whether the number was found in the segment.
68+
*
69+
* @return true if the number was found, false otherwise
70+
*/
6871
boolean getResult() {
6972
return found;
7073
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class LinearSearchThreadTest {
9+
10+
/**
11+
* Test for finding an element that is present in the array.
12+
*/
13+
@Test
14+
void testSearcherFound() throws InterruptedException {
15+
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
16+
Searcher searcher = new Searcher(array, 0, array.length, 5);
17+
searcher.start();
18+
searcher.join();
19+
assertTrue(searcher.getResult(), "The element 5 should be found in the array.");
20+
}
21+
22+
/**
23+
* Test for finding an element that is not present in the array.
24+
*/
25+
@Test
26+
void testSearcherNotFound() throws InterruptedException {
27+
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
28+
Searcher searcher = new Searcher(array, 0, array.length, 11);
29+
searcher.start();
30+
searcher.join();
31+
assertFalse(searcher.getResult(), "The element 11 should not be found in the array.");
32+
}
33+
34+
/**
35+
* Test for searching a segment of the array.
36+
*/
37+
@Test
38+
void testSearcherSegmentFound() throws InterruptedException {
39+
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
40+
Searcher searcher = new Searcher(array, 0, 5, 3);
41+
searcher.start();
42+
searcher.join();
43+
assertTrue(searcher.getResult(), "The element 3 should be found in the segment.");
44+
}
45+
46+
/**
47+
* Test for searching an empty array segment.
48+
*/
49+
@Test
50+
void testSearcherEmptySegment() throws InterruptedException {
51+
int[] array = {};
52+
Searcher searcher = new Searcher(array, 0, 0, 1); // Empty array
53+
searcher.start();
54+
searcher.join();
55+
assertFalse(searcher.getResult(), "The element should not be found in an empty segment.");
56+
}
57+
58+
/**
59+
* Test for searching with random numbers.
60+
*/
61+
@Test
62+
void testSearcherRandomNumbers() throws InterruptedException {
63+
int size = 200;
64+
int[] array = new int[size];
65+
for (int i = 0; i < size; i++) {
66+
array[i] = (int) (Math.random() * 100);
67+
}
68+
int target = array[(int) (Math.random() * size)]; // Randomly select a target that is present
69+
Searcher searcher = new Searcher(array, 0, size, target);
70+
searcher.start();
71+
searcher.join();
72+
assertTrue(searcher.getResult(), "The randomly selected target should be found in the array.");
73+
}
74+
}

0 commit comments

Comments
 (0)