Skip to content

Commit adeb454

Browse files
authored
Merge branch 'master' into SMJI/stack/min_max_const_time
2 parents dd8331d + 1c978c5 commit adeb454

File tree

5 files changed

+292
-1
lines changed

5 files changed

+292
-1
lines changed

DIRECTORY.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
5757
* [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java)
5858
* [DES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/DES.java)
59+
* [ECC](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ECC.java)
5960
* [HillCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/HillCipher.java)
6061
* [PlayfairCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java)
6162
* [Polybius](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Polybius.java)
@@ -275,6 +276,7 @@
275276
* [ShortestCommonSupersequenceLength](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java)
276277
* [SubsetCount](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java)
277278
* [SubsetSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java)
279+
* [SubsetSumSpaceOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java)
278280
* [SumOfSubset](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java)
279281
* [Tribonacci](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Tribonacci.java)
280282
* [UniquePaths](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java)
@@ -396,6 +398,7 @@
396398
* [SecondMinMax](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SecondMinMax.java)
397399
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java)
398400
* [SimpsonIntegration](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SimpsonIntegration.java)
401+
* [SolovayStrassenPrimalityTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SolovayStrassenPrimalityTest.java)
399402
* [SquareFreeInteger](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SquareFreeInteger.java)
400403
* [SquareRootWithBabylonianMethod](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java)
401404
* [SquareRootWithNewtonRaphsonMethod](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java)
@@ -511,6 +514,7 @@
511514
* [PerfectBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)
512515
* [QuickSelect](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/QuickSelect.java)
513516
* [RabinKarpAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java)
517+
* [RandomSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RandomSearch.java)
514518
* [RecursiveBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java)
515519
* [RowColumnWiseSorted2dArrayBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java)
516520
* [SaddlebackSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java)
@@ -664,12 +668,13 @@
664668
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
665669
* [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
666670
* [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java)
667-
* [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashTest.java)
671+
* [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AtbashTest.java)
668672
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
669673
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
670674
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
671675
* [ColumnarTranspositionCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java)
672676
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
677+
* [ECCTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ECCTest.java)
673678
* [HillCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/HillCipherTest.java)
674679
* [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java)
675680
* [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java)
@@ -829,6 +834,7 @@
829834
* [RodCuttingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java)
830835
* [ShortestCommonSupersequenceLengthTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLengthTest.java)
831836
* [SubsetCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java)
837+
* [SubsetSumSpaceOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java)
832838
* [SubsetSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumTest.java)
833839
* [SumOfSubsetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java)
834840
* [TribonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/TribonacciTest.java)
@@ -935,6 +941,7 @@
935941
* [ReverseNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ReverseNumberTest.java)
936942
* [SecondMinMaxTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java)
937943
* [SieveOfEratosthenesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java)
944+
* [SolovayStrassenPrimalityTestTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SolovayStrassenPrimalityTestTest.java)
938945
* [SquareFreeIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SquareFreeIntegerTest.java)
939946
* [SquareRootwithBabylonianMethodTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SquareRootwithBabylonianMethodTest.java)
940947
* [SquareRootWithNewtonRaphsonTestMethod](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java)
@@ -1018,6 +1025,7 @@
10181025
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
10191026
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
10201027
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
1028+
* [RandomSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RandomSearchTest.java)
10211029
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
10221030
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
10231031
* [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.math.BigInteger;
4+
5+
/**
6+
* This class provides an implementation of the Karatsuba multiplication algorithm.
7+
*
8+
* <p>
9+
* Karatsuba multiplication is a divide-and-conquer algorithm for multiplying two large
10+
* numbers. It is faster than the classical multiplication algorithm and reduces the
11+
* time complexity to O(n^1.585) by breaking the multiplication of two n-digit numbers
12+
* into three multiplications of n/2-digit numbers.
13+
* </p>
14+
*
15+
* <p>
16+
* The main idea of the Karatsuba algorithm is based on the following observation:
17+
* </p>
18+
*
19+
* <pre>
20+
* Let x and y be two numbers:
21+
* x = a * 10^m + b
22+
* y = c * 10^m + d
23+
*
24+
* Then, the product of x and y can be expressed as:
25+
* x * y = (a * c) * 10^(2*m) + ((a * d) + (b * c)) * 10^m + (b * d)
26+
* </pre>
27+
*
28+
* The Karatsuba algorithm calculates this more efficiently by reducing the number of
29+
* multiplications from four to three by using the identity:
30+
*
31+
* <pre>
32+
* (a + b)(c + d) = ac + ad + bc + bd
33+
* </pre>
34+
*
35+
* <p>
36+
* The recursion continues until the numbers are small enough to multiply directly using
37+
* the traditional method.
38+
* </p>
39+
*/
40+
public final class KaratsubaMultiplication {
41+
42+
/**
43+
* Private constructor to hide the implicit public constructor
44+
*/
45+
private KaratsubaMultiplication() {
46+
}
47+
48+
/**
49+
* Multiplies two large numbers using the Karatsuba algorithm.
50+
*
51+
* <p>
52+
* This method recursively splits the numbers into smaller parts until they are
53+
* small enough to be multiplied directly using the traditional method.
54+
* </p>
55+
*
56+
* @param x The first large number to be multiplied (BigInteger).
57+
* @param y The second large number to be multiplied (BigInteger).
58+
* @return The product of the two numbers (BigInteger).
59+
*/
60+
public static BigInteger karatsuba(BigInteger x, BigInteger y) {
61+
// Base case: when numbers are small enough, use direct multiplication
62+
// If the number is 4 bits or smaller, switch to the classical method
63+
if (x.bitLength() <= 4 || y.bitLength() <= 4) {
64+
return x.multiply(y);
65+
}
66+
67+
// Find the maximum bit length of the two numbers
68+
int n = Math.max(x.bitLength(), y.bitLength());
69+
70+
// Split the numbers in the middle
71+
int m = n / 2;
72+
73+
// High and low parts of the first number x (x = a * 10^m + b)
74+
BigInteger high1 = x.shiftRight(m); // a = x / 2^m (higher part)
75+
BigInteger low1 = x.subtract(high1.shiftLeft(m)); // b = x - a * 2^m (lower part)
76+
77+
// High and low parts of the second number y (y = c * 10^m + d)
78+
BigInteger high2 = y.shiftRight(m); // c = y / 2^m (higher part)
79+
BigInteger low2 = y.subtract(high2.shiftLeft(m)); // d = y - c * 2^m (lower part)
80+
81+
// Recursively calculate three products
82+
BigInteger z0 = karatsuba(low1, low2); // z0 = b * d (low1 * low2)
83+
BigInteger z1 = karatsuba(low1.add(high1), low2.add(high2)); // z1 = (a + b) * (c + d)
84+
BigInteger z2 = karatsuba(high1, high2); // z2 = a * c (high1 * high2)
85+
86+
// Combine the results using Karatsuba's formula
87+
// z0 + ((z1 - z2 - z0) << m) + (z2 << 2m)
88+
return z2
89+
.shiftLeft(2 * m) // z2 * 10^(2*m)
90+
.add(z1.subtract(z2).subtract(z0).shiftLeft(m)) // (z1 - z2 - z0) * 10^m
91+
.add(z0); // z0
92+
}
93+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.searches;
2+
3+
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
import java.util.HashSet;
5+
import java.util.Random;
6+
import java.util.Set;
7+
8+
/**
9+
* A Random Search algorithm that randomly selects an index and checks if the
10+
* value at that index matches the target. It repeats the process until it
11+
* finds the target or checks all elements.
12+
*
13+
* <p>
14+
* Time Complexity: O(n) in the worst case.
15+
* </p>
16+
*
17+
* @author Hardvan
18+
*/
19+
public class RandomSearch implements SearchAlgorithm {
20+
21+
private final Random random = new Random();
22+
23+
/**
24+
* Finds the index of a given element using random search.
25+
*
26+
* @param array Array to search through
27+
* @param key Element to search for
28+
* @return Index of the element if found, -1 otherwise
29+
*/
30+
@Override
31+
public <T extends Comparable<T>> int find(T[] array, T key) {
32+
Set<Integer> visitedIndices = new HashSet<>();
33+
int size = array.length;
34+
35+
while (visitedIndices.size() < size) {
36+
int randomIndex = random.nextInt(size);
37+
if (array[randomIndex].compareTo(key) == 0) {
38+
return randomIndex;
39+
}
40+
visitedIndices.add(randomIndex);
41+
}
42+
43+
return -1;
44+
}
45+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.math.BigInteger;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
/**
12+
* Unit test class for {@link KaratsubaMultiplication} class.
13+
*
14+
* <p>
15+
* This class tests various edge cases and normal cases for the
16+
* Karatsuba multiplication algorithm implemented in the KaratsubaMultiplication class.
17+
* It uses parameterized tests to handle multiple test cases.
18+
* </p>
19+
*/
20+
class KaratsubaMultiplicationTest {
21+
22+
/**
23+
* Provides test data for the parameterized test.
24+
* Each entry in the stream contains three elements: x, y, and the expected result.
25+
*
26+
* @return a stream of arguments for the parameterized test
27+
*/
28+
static Stream<Arguments> provideTestCases() {
29+
return Stream.of(
30+
// Test case 1: Two small numbers
31+
Arguments.of(new BigInteger("1234"), new BigInteger("5678"), new BigInteger("7006652")),
32+
// Test case 2: Two large numbers
33+
Arguments.of(new BigInteger("342364"), new BigInteger("393958"), new BigInteger("134877036712")),
34+
// Test case 3: One number is zero
35+
Arguments.of(BigInteger.ZERO, new BigInteger("5678"), BigInteger.ZERO),
36+
// Test case 4: Both numbers are zero
37+
Arguments.of(BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO),
38+
// Test case 5: Single-digit numbers
39+
Arguments.of(new BigInteger("9"), new BigInteger("8"), new BigInteger("72")));
40+
}
41+
42+
/**
43+
* Parameterized test for Karatsuba multiplication.
44+
*
45+
* <p>
46+
* This method runs the Karatsuba multiplication algorithm for multiple test cases.
47+
* </p>
48+
*
49+
* @param x the first number to multiply
50+
* @param y the second number to multiply
51+
* @param expected the expected result of x * y
52+
*/
53+
@ParameterizedTest
54+
@MethodSource("provideTestCases")
55+
void testKaratsubaMultiplication(BigInteger x, BigInteger y, BigInteger expected) {
56+
assertEquals(expected, KaratsubaMultiplication.karatsuba(x, y));
57+
}
58+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
class RandomSearchTest {
10+
11+
private RandomSearch randomSearch;
12+
13+
@BeforeEach
14+
void setUp() {
15+
randomSearch = new RandomSearch();
16+
}
17+
18+
@Test
19+
void testElementFound() {
20+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
21+
Integer key = 5;
22+
int index = randomSearch.find(array, key);
23+
24+
assertNotEquals(-1, index, "Element should be found in the array.");
25+
assertEquals(key, array[index], "Element found should match the key.");
26+
}
27+
28+
@Test
29+
void testElementNotFound() {
30+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
31+
Integer key = 11;
32+
int index = randomSearch.find(array, key);
33+
34+
assertEquals(-1, index, "Element not present in the array should return -1.");
35+
}
36+
37+
@Test
38+
void testEmptyArray() {
39+
Integer[] emptyArray = {};
40+
Integer key = 5;
41+
int index = randomSearch.find(emptyArray, key);
42+
43+
assertEquals(-1, index, "Searching in an empty array should return -1.");
44+
}
45+
46+
@Test
47+
void testSingleElementArrayFound() {
48+
Integer[] array = {5};
49+
Integer key = 5;
50+
int index = randomSearch.find(array, key);
51+
52+
assertEquals(0, index, "The key should be found at index 0 in a single-element array.");
53+
}
54+
55+
@Test
56+
void testSingleElementArrayNotFound() {
57+
Integer[] array = {1};
58+
Integer key = 5;
59+
int index = randomSearch.find(array, key);
60+
61+
assertEquals(-1, index, "The key should not be found in a single-element array if it does not match.");
62+
}
63+
64+
@Test
65+
void testDuplicateElementsFound() {
66+
Integer[] array = {1, 2, 3, 4, 5, 5, 5, 7, 8, 9, 10};
67+
Integer key = 5;
68+
int index = randomSearch.find(array, key);
69+
70+
assertNotEquals(-1, index, "The key should be found in the array with duplicates.");
71+
assertEquals(key, array[index], "The key found should be 5.");
72+
}
73+
74+
@Test
75+
void testLargeArray() {
76+
Integer[] largeArray = new Integer[1000];
77+
for (int i = 0; i < largeArray.length; i++) {
78+
largeArray[i] = i + 1; // Fill with values 1 to 1000
79+
}
80+
81+
Integer key = 500;
82+
int index = randomSearch.find(largeArray, key);
83+
84+
assertNotEquals(-1, index, "The key should be found in the large array.");
85+
assertEquals(key, largeArray[index], "The key found should match 500.");
86+
}
87+
}

0 commit comments

Comments
 (0)