Skip to content

Commit 0f31aa7

Browse files
authored
Merge branch 'master' into solovay-strassen
2 parents 0b8809a + b1aeac5 commit 0f31aa7

File tree

9 files changed

+392
-116
lines changed

9 files changed

+392
-116
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,18 +1010,22 @@
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)
10161017
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
10171018
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
10181019
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
10191020
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
1021+
* [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
10201022
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
10211023
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
10221024
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
1025+
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
10231026
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
10241027
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
1028+
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
10251029
* sorts
10261030
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)
10271031
* [BinaryInsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.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: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Program to perform Saddleback Search Given a sorted 2D array(elements are
75
* sorted across every row and column, assuming ascending order) of size n*m we
@@ -27,10 +25,15 @@ private SaddlebackSearch() {
2725
* @param row the current row.
2826
* @param col the current column.
2927
* @param key the element that we want to search for.
28+
* @throws IllegalArgumentException if the array is empty.
3029
* @return The index(row and column) of the element if found. Else returns
3130
* -1 -1.
3231
*/
33-
private static int[] find(int[][] arr, int row, int col, int key) {
32+
static int[] find(int[][] arr, int row, int col, int key) {
33+
if (arr.length == 0) {
34+
throw new IllegalArgumentException("Array is empty");
35+
}
36+
3437
// array to store the answer row and column
3538
int[] ans = {-1, -1};
3639
if (row < 0 || col >= arr[row].length) {
@@ -47,30 +50,4 @@ else if (arr[row][col] > key) {
4750
// else we move right
4851
return find(arr, row, col + 1, key);
4952
}
50-
51-
/**
52-
* Main method
53-
*
54-
* @param args Command line arguments
55-
*/
56-
public static void main(String[] args) {
57-
// TODO Auto-generated method stub
58-
Scanner sc = new Scanner(System.in);
59-
int[][] arr;
60-
int i;
61-
int j;
62-
int rows = sc.nextInt();
63-
int col = sc.nextInt();
64-
arr = new int[rows][col];
65-
for (i = 0; i < rows; i++) {
66-
for (j = 0; j < col; j++) {
67-
arr[i][j] = sc.nextInt();
68-
}
69-
}
70-
int ele = sc.nextInt();
71-
// we start from bottom left corner
72-
int[] ans = find(arr, rows - 1, 0, ele);
73-
System.out.println(ans[0] + " " + ans[1]);
74-
sc.close();
75-
}
7653
}

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
import java.util.Arrays;
5-
import java.util.Random;
6-
import java.util.stream.Stream;
74

85
/**
96
* A ternary search algorithm is a technique in computer science for finding the
@@ -60,23 +57,4 @@ private <T extends Comparable<T>> int ternarySearch(T[] arr, T key, int start, i
6057
return ternarySearch(arr, key, mid1, mid2);
6158
}
6259
}
63-
64-
public static void main(String[] args) {
65-
// just generate data
66-
Random r = new Random();
67-
int size = 100;
68-
int maxElement = 100000;
69-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
70-
71-
// the element that should be found
72-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
73-
74-
TernarySearch search = new TernarySearch();
75-
int atIndex = search.find(integers, shouldBeFound);
76-
77-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
78-
79-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
80-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
81-
}
8260
}

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
import java.util.Random;
5-
import java.util.concurrent.ThreadLocalRandom;
6-
import java.util.stream.IntStream;
74

85
/**
96
* The UpperBound method is used to return an index pointing to the first
@@ -25,28 +22,6 @@
2522
*/
2623
class UpperBound implements SearchAlgorithm {
2724

28-
// Driver Program
29-
public static void main(String[] args) {
30-
// Just generate data
31-
Random r = ThreadLocalRandom.current();
32-
33-
int size = 100;
34-
int maxElement = 100000;
35-
36-
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
37-
38-
// The element for which the upper bound is to be found
39-
int val = integers[r.nextInt(size - 1)] + 1;
40-
41-
UpperBound search = new UpperBound();
42-
int atIndex = search.find(integers, val);
43-
44-
System.out.printf("Val: %d. Upper Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
45-
46-
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
47-
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
48-
}
49-
5025
/**
5126
* @param array is an array where the UpperBound value is to be found
5227
* @param key is an element for which the UpperBound is to be found
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)