Skip to content

Commit d6090a5

Browse files
committed
2 parents 9c42a5f + b10e451 commit d6090a5

File tree

9 files changed

+201
-50
lines changed

9 files changed

+201
-50
lines changed

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
3434
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
3535
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
36+
* [OnesComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java)
3637
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
3738
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
39+
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
3840
* ciphers
3941
* a5
4042
* [A5Cipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/A5Cipher.java)
@@ -185,6 +187,7 @@
185187
* [AVLSimple](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java)
186188
* [AVLTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/AVLTree.java)
187189
* [BinaryTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java)
190+
* [BoundaryTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BoundaryTraversal.java)
188191
* [BSTFromSortedArray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BSTFromSortedArray.java)
189192
* [BSTIterative](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BSTIterative.java)
190193
* [BSTRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/BSTRecursive.java)
@@ -643,8 +646,10 @@
643646
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
644647
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
645648
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
649+
* [OnesComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java)
646650
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
647651
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
652+
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
648653
* ciphers
649654
* a5
650655
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
@@ -754,6 +759,7 @@
754759
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
755760
* trees
756761
* [BinaryTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java)
762+
* [BoundaryTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BoundaryTraversalTest.java)
757763
* [BSTFromSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BSTFromSortedArrayTest.java)
758764
* [BSTIterativeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BSTIterativeTest.java)
759765
* [BSTRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BSTRecursiveTest.java)
@@ -783,6 +789,7 @@
783789
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
784790
* dynamicprogramming
785791
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
792+
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
786793
* [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
787794
* [CatalanNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CatalanNumberTest.java)
788795
* [ClimbStairsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ClimbStairsTest.java)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class to find the single non-duplicate element from an array
5+
* where all other elements appear twice.
6+
* <p>
7+
* The algorithm runs in O(n) time complexity and O(1) space complexity
8+
* using bitwise XOR.
9+
* </p>
10+
*
11+
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
12+
*/
13+
public final class SingleElement {
14+
15+
/**
16+
* Private constructor to prevent instantiation of this utility class.
17+
* Throws an UnsupportedOperationException if attempted.
18+
*/
19+
private SingleElement() {
20+
throw new UnsupportedOperationException("Utility Class");
21+
}
22+
23+
/**
24+
* Finds the single non-duplicate element in an array where every other
25+
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
26+
* complexity and O(1) space complexity.
27+
*
28+
* @param arr the input array containing integers where every element
29+
* except one appears exactly twice
30+
* @return the single non-duplicate element
31+
*/
32+
public static int findSingleElement(int[] arr) {
33+
int ele = 0;
34+
for (int i = 0; i < arr.length; i++) {
35+
ele ^= arr[i];
36+
}
37+
return ele;
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Swap every pair of adjacent bits of a given number.
5+
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
6+
*/
7+
8+
public final class SwapAdjacentBits {
9+
private SwapAdjacentBits() {
10+
}
11+
12+
public static int swapAdjacentBits(int num) {
13+
// mask the even bits (0xAAAAAAAA => 10101010...)
14+
int evenBits = num & 0xAAAAAAAA;
15+
16+
// mask the odd bits (0x55555555 => 01010101...)
17+
int oddBits = num & 0x55555555;
18+
19+
// right shift even bits and left shift odd bits
20+
evenBits >>= 1;
21+
oddBits <<= 1;
22+
23+
// combine shifted bits
24+
return evenBits | oddBits;
25+
}
26+
}

src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,48 +52,4 @@ public static void boundaryFill(int[][] image, int xCoordinate, int yCoordinate,
5252
boundaryFill(image, xCoordinate - 1, yCoordinate - 1, newColor, boundaryColor);
5353
}
5454
}
55-
56-
/**
57-
* This method will print the 2D image matrix
58-
*
59-
* @param image The image to be printed on the console
60-
*/
61-
public static void printImageArray(int[][] image) {
62-
for (int i = 0; i < image.length; i++) {
63-
for (int j = 0; j < image[0].length; j++) {
64-
System.out.print(image[i][j] + " ");
65-
}
66-
67-
System.out.println();
68-
}
69-
}
70-
71-
// Driver Program
72-
public static void main(String[] args) {
73-
// Input 2D image matrix
74-
int[][] image = {
75-
{0, 0, 0, 0, 0, 0, 0},
76-
{0, 3, 3, 3, 3, 0, 0},
77-
{0, 3, 0, 0, 3, 0, 0},
78-
{0, 3, 0, 0, 3, 3, 3},
79-
{0, 3, 3, 3, 0, 0, 3},
80-
{0, 0, 0, 3, 0, 0, 3},
81-
{0, 0, 0, 3, 3, 3, 3},
82-
};
83-
84-
boundaryFill(image, 2, 2, 5, 3);
85-
86-
/* Output ==>
87-
* 0 0 0 0 0 0 0
88-
0 3 3 3 3 0 0
89-
0 3 5 5 3 0 0
90-
0 3 5 5 3 3 3
91-
0 3 3 3 5 5 3
92-
0 0 0 3 5 5 3
93-
0 0 0 3 3 3 3
94-
* */
95-
96-
// print 2D image matrix
97-
printImageArray(image);
98-
}
9955
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,6 @@ private double computeBM25Score(int termFrequency, double docLength, double idf)
215215
*/
216216
private double computeIDF(int docFrequency) {
217217
// Total number of documents in the index
218-
return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5));
218+
return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5) + 1);
219219
}
220220
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public final class SingleElementTest {
11+
12+
/**
13+
* Parameterized test to find the single non-duplicate element
14+
* in the given arrays.
15+
*
16+
* @param arr the input array where every element appears twice except one
17+
* @param expected the expected single element result
18+
*/
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void testFindSingleElement(int[] arr, int expected) {
22+
assertEquals(expected, SingleElement.findSingleElement(arr));
23+
}
24+
25+
/**
26+
* Provides test cases for the parameterized test.
27+
*
28+
* @return Stream of arguments consisting of arrays and expected results
29+
*/
30+
private static Stream<Arguments> provideTestCases() {
31+
return Stream.of(Arguments.of(new int[] {1, 1, 2, 2, 4, 4, 3}, 3), Arguments.of(new int[] {1, 2, 2, 3, 3}, 1), Arguments.of(new int[] {10}, 10));
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
class SwapAdjacentBitsTest {
9+
10+
@ParameterizedTest
11+
@CsvSource({
12+
"2, 1", // 2 (10 in binary) should become 1 (01 in binary)
13+
"43, 23", // 43 should become 23
14+
"153, 102", // 153 should become 102
15+
"15, 15", // 15 (1111) remains 15 (1111)
16+
"0, 0" // 0 (0000) remains 0 (0000)
17+
})
18+
void
19+
testSwapAdjacentBits(int input, int expected) {
20+
assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input));
21+
}
22+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BoundaryFillTest {
10+
11+
private int[][] image;
12+
13+
@BeforeEach
14+
void setUp() {
15+
image = new int[][] {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
16+
}
17+
18+
@Test
19+
void testGetPixel() {
20+
assertEquals(3, BoundaryFill.getPixel(image, 1, 1));
21+
assertEquals(0, BoundaryFill.getPixel(image, 2, 2));
22+
assertEquals(3, BoundaryFill.getPixel(image, 4, 3));
23+
}
24+
25+
@Test
26+
void testPutPixel() {
27+
BoundaryFill.putPixel(image, 2, 2, 5);
28+
assertEquals(5, BoundaryFill.getPixel(image, 2, 2));
29+
30+
BoundaryFill.putPixel(image, 0, 0, 7);
31+
assertEquals(7, BoundaryFill.getPixel(image, 0, 0));
32+
}
33+
34+
@Test
35+
void testBoundaryFill() {
36+
BoundaryFill.boundaryFill(image, 2, 2, 5, 3);
37+
38+
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 5, 5, 3, 0, 0}, {0, 3, 5, 5, 3, 3, 3}, {0, 3, 3, 3, 5, 5, 3}, {0, 0, 0, 3, 5, 5, 3}, {0, 0, 0, 3, 3, 3, 3}};
39+
40+
for (int i = 0; i < image.length; i++) {
41+
assertArrayEquals(expectedImage[i], image[i]);
42+
}
43+
}
44+
45+
@Test
46+
void testBoundaryFillEdgeCase() {
47+
BoundaryFill.boundaryFill(image, 1, 1, 3, 3);
48+
49+
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
50+
51+
for (int i = 0; i < image.length; i++) {
52+
assertArrayEquals(expectedImage[i], image[i]);
53+
}
54+
}
55+
56+
@Test
57+
void testBoundaryFillInvalidCoordinates() {
58+
BoundaryFill.boundaryFill(image, -1, -1, 5, 3);
59+
60+
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
61+
62+
for (int i = 0; i < image.length; i++) {
63+
assertArrayEquals(expectedImage[i], image[i]);
64+
}
65+
}
66+
}

src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ void testSearchRanking() {
5050
// Perform search for the term "good"
5151
List<SearchResult> results = index.search("good");
5252
assertFalse(results.isEmpty());
53-
53+
for (SearchResult result : results) {
54+
System.out.println(result);
55+
}
5456
// Validate the ranking based on the provided relevance scores
55-
assertEquals(6, results.get(0).getDocId()); // It's a Wonderful Life should be ranked 1st
56-
assertEquals(7, results.get(1).getDocId()); // The Pursuit of Happyness should be ranked 2nd
57+
assertEquals(1, results.get(0).getDocId()); // The Shawshank Redemption should be ranked 1st
58+
assertEquals(8, results.get(1).getDocId()); // A Few Good Men should be ranked 2nd
5759
assertEquals(5, results.get(2).getDocId()); // Good Will Hunting should be ranked 3rd
58-
assertEquals(8, results.get(3).getDocId()); // A Few Good Men should be ranked 4th
59-
assertEquals(1, results.get(4).getDocId()); // The Shawshank Redemption should be ranked 5th
60+
assertEquals(7, results.get(3).getDocId()); // The Pursuit of Happyness should be ranked 4th
61+
assertEquals(6, results.get(4).getDocId()); // It's a Wonderful Life should be ranked 5th
6062

6163
// Ensure the relevance scores are in descending order
6264
for (int i = 0; i < results.size() - 1; i++) {

0 commit comments

Comments
 (0)