1
1
package com .thealgorithms .searches ;
2
2
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
+ */
5
21
public final class LinearSearchThread {
6
22
private LinearSearchThread () {
7
23
}
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
- }
40
24
}
41
25
26
+ /**
27
+ * The Searcher class extends Thread and is responsible for searching for a specific
28
+ * number in a segment of an array.
29
+ */
42
30
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
43
36
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
+ */
50
45
Searcher (int [] arr , int left , int right , int x ) {
51
46
this .arr = arr ;
52
47
this .left = left ;
53
48
this .right = right ;
54
49
this .x = x ;
55
50
}
56
51
52
+ /**
53
+ * The run method for the thread, performing the linear search in its segment.
54
+ */
57
55
@ Override
58
56
public void run () {
59
57
int k = left ;
@@ -65,6 +63,11 @@ public void run() {
65
63
}
66
64
}
67
65
66
+ /**
67
+ * Returns whether the number was found in the segment.
68
+ *
69
+ * @return true if the number was found, false otherwise
70
+ */
68
71
boolean getResult () {
69
72
return found ;
70
73
}
0 commit comments