Skip to content

Commit 3fb9a74

Browse files
Merge branch 'master' into patch-1
2 parents 06e717e + 96ed2d7 commit 3fb9a74

6 files changed

Lines changed: 230 additions & 34 deletions

File tree

‎src/main/java/com/thealgorithms/searches/JumpSearch.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* @see BinarySearch
5757
* @see LinearSearch
5858
*/
59-
public class JumpSearch implements SearchAlgorithm {
59+
public final class JumpSearch implements SearchAlgorithm {
6060

6161
/**
6262
* Jump Search algorithm implementation.
@@ -67,6 +67,10 @@ public class JumpSearch implements SearchAlgorithm {
6767
*/
6868
@Override
6969
public <T extends Comparable<T>> int find(T[] array, T key) {
70+
if (array == null || array.length == 0 || key == null) {
71+
return -1;
72+
}
73+
7074
int length = array.length;
7175
int blockSize = (int) Math.sqrt(length);
7276

@@ -78,7 +82,7 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
7882

7983
// Perform linear search within the identified block
8084
for (int i = limit - blockSize; i <= limit && i < length; i++) {
81-
if (array[i].equals(key)) {
85+
if (array[i] != null && key.compareTo(array[i]) == 0) {
8286
return i;
8387
}
8488
}
Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
package com.thealgorithms.searches;
2-
3-
public class SearchInARowAndColWiseSortedMatrix {
4-
/**
5-
* Search a key in row and column wise sorted matrix
6-
*
7-
* @param matrix matrix to be searched
8-
* @param value Key being searched for
9-
* @author Sadiul Hakim : https://github.com/sadiul-hakim
10-
*/
11-
public int[] search(int[][] matrix, int value) {
12-
int n = matrix.length;
13-
// This variable iterates over rows
14-
int i = 0;
15-
// This variable iterates over columns
16-
int j = n - 1;
17-
int[] result = {-1, -1};
18-
while (i < n && j >= 0) {
19-
if (matrix[i][j] == value) {
20-
result[0] = i;
21-
result[1] = j;
22-
return result;
23-
}
24-
if (value > matrix[i][j]) {
25-
i++;
26-
} else {
27-
j--;
28-
}
29-
}
30-
return result;
31-
}
32-
}
1+
package com.thealgorithms.searches;
2+
3+
public final class SearchInARowAndColWiseSortedMatrix {
4+
/**
5+
* Search a key in row and column wise sorted matrix
6+
*
7+
* @param matrix matrix to be searched
8+
* @param value Key being searched for
9+
* @return row and column indices if found, otherwise {-1, -1}
10+
* @author Sadiul Hakim : https://github.com/sadiul-hakim
11+
*/
12+
public int[] search(int[][] matrix, int value) {
13+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
14+
return new int[] {-1, -1};
15+
}
16+
17+
int rows = matrix.length;
18+
int cols = matrix[0].length;
19+
// This variable iterates over rows
20+
int i = 0;
21+
// This variable iterates over columns, starting from top-right corner
22+
int j = cols - 1;
23+
int[] result = {-1, -1};
24+
while (i < rows && j >= 0) {
25+
if (matrix[i] == null || j >= matrix[i].length) {
26+
i++;
27+
continue;
28+
}
29+
if (matrix[i][j] == value) {
30+
result[0] = i;
31+
result[1] = j;
32+
return result;
33+
}
34+
if (value > matrix[i][j]) {
35+
i++;
36+
} else {
37+
j--;
38+
}
39+
}
40+
return result;
41+
}
42+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Finds the lexicographically smallest cyclic rotation of a string using
5+
* Booth's algorithm.
6+
*
7+
* <p>The algorithm compares candidate rotations without materializing them,
8+
* running in O(n) time and O(1) auxiliary space.</p>
9+
*
10+
* @see <a href="https://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation">
11+
* Lexicographically minimal string rotation</a>
12+
*/
13+
public final class BoothsAlgorithm {
14+
private BoothsAlgorithm() {
15+
}
16+
17+
/**
18+
* Returns the starting index of the lexicographically smallest cyclic
19+
* rotation of {@code input}. If several rotations are equal and minimal,
20+
* the algorithm returns the smallest starting index among them.
21+
*
22+
* <p>For example, the smallest rotation of {@code "baca"} is
23+
* {@code "abac"}, which starts at index {@code 3}.</p>
24+
*
25+
* @param input the string whose minimal cyclic rotation is sought
26+
* @return the starting index of the minimal rotation, or {@code 0} for an
27+
* empty string
28+
* @throws IllegalArgumentException if {@code input} is {@code null}
29+
*/
30+
public static int minimalRotationIndex(String input) {
31+
if (input == null) {
32+
throw new IllegalArgumentException("Input string must not be null");
33+
}
34+
35+
int length = input.length();
36+
if (length == 0) {
37+
return 0;
38+
}
39+
40+
int first = 0;
41+
int second = 1;
42+
int offset = 0;
43+
44+
while (first < length && second < length && offset < length) {
45+
char firstChar = input.charAt((first + offset) % length);
46+
char secondChar = input.charAt((second + offset) % length);
47+
48+
if (firstChar == secondChar) {
49+
offset++;
50+
continue;
51+
}
52+
53+
if (firstChar > secondChar) {
54+
first += offset + 1;
55+
if (first <= second) {
56+
first = second + 1;
57+
}
58+
} else {
59+
second += offset + 1;
60+
if (second <= first) {
61+
second = first + 1;
62+
}
63+
}
64+
offset = 0;
65+
}
66+
67+
return Math.min(first, second);
68+
}
69+
}

‎src/test/java/com/thealgorithms/searches/JumpSearchTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,40 @@ void testJumpSearchFindsEveryElement() {
139139
}
140140
}
141141
}
142+
143+
@Test
144+
void testJumpSearchNullArray() {
145+
JumpSearch jumpSearch = new JumpSearch();
146+
assertEquals(-1, jumpSearch.find(null, 5), "Searching in a null array should return -1.");
147+
}
148+
149+
@Test
150+
void testJumpSearchNullKey() {
151+
JumpSearch jumpSearch = new JumpSearch();
152+
Integer[] array = {1, 2, 3};
153+
assertEquals(-1, jumpSearch.find(array, null), "Searching for a null key should return -1.");
154+
}
155+
156+
@Test
157+
void testJumpSearchSingleElementAbsent() {
158+
JumpSearch jumpSearch = new JumpSearch();
159+
Integer[] array = {10};
160+
assertEquals(-1, jumpSearch.find(array, 5), "Searching for an absent key in single element array should return -1.");
161+
}
162+
163+
@Test
164+
void testJumpSearchStringArray() {
165+
JumpSearch jumpSearch = new JumpSearch();
166+
String[] array = {"apple", "banana", "cherry", "date", "elderberry"};
167+
assertEquals(2, jumpSearch.find(array, "cherry"), "The index of 'cherry' should be 2.");
168+
assertEquals(-1, jumpSearch.find(array, "fig"), "Key 'fig' should not be found.");
169+
}
170+
171+
@Test
172+
void testJumpSearchDuplicates() {
173+
JumpSearch jumpSearch = new JumpSearch();
174+
Integer[] array = {1, 2, 2, 2, 3, 4, 5};
175+
int index = jumpSearch.find(array, 2);
176+
assertEquals(2, array[index], "The element at the returned index should equal the target key.");
177+
}
142178
}

‎src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,36 @@ void testSearchInColWiseSortedMatrix() {
6363
int[] expected = {1, 1}; // Found at (1, 1)
6464
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix");
6565
}
66+
67+
@Test
68+
void testSearchInNullMatrix() {
69+
assertArrayEquals(new int[] {-1, -1}, searcher.search(null, 5), "Should return {-1, -1} for null matrix");
70+
}
71+
72+
@Test
73+
void testSearchInMatrixWithEmptyRows() {
74+
int[][] matrix = {{}};
75+
assertArrayEquals(new int[] {-1, -1}, searcher.search(matrix, 5), "Should return {-1, -1} for matrix with empty first row");
76+
}
77+
78+
@Test
79+
void testSearchInRectangularMatrixMoreColsThanRowsFound() {
80+
int[][] matrix = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
81+
int[] expected = {0, 4}; // Value 9 is at row 0, col 4
82+
assertArrayEquals(expected, searcher.search(matrix, 9), "Value 9 should be found at row 0, col 4");
83+
}
84+
85+
@Test
86+
void testSearchInRectangularMatrixMoreRowsThanColsFound() {
87+
int[][] matrix = {{1, 5}, {2, 6}, {3, 7}, {4, 8}};
88+
int[] expected = {2, 0}; // Value 3 is at row 2, col 0
89+
assertArrayEquals(expected, searcher.search(matrix, 3), "Value 3 should be found at row 2, col 0");
90+
}
91+
92+
@Test
93+
void testSearchInRectangularMatrixNotFound() {
94+
int[][] matrix = {{1, 3, 5}, {2, 4, 6}, {7, 8, 9}, {10, 11, 12}};
95+
int[] expected = {-1, -1};
96+
assertArrayEquals(expected, searcher.search(matrix, 15), "Value 15 should not be found");
97+
}
6698
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class BoothsAlgorithmTest {
9+
10+
@Test
11+
public void testMinimalRotationIndexForExample() {
12+
assertEquals(3, BoothsAlgorithm.minimalRotationIndex("baca"));
13+
}
14+
15+
@Test
16+
public void testEmptyString() {
17+
assertEquals(0, BoothsAlgorithm.minimalRotationIndex(""));
18+
}
19+
20+
@Test
21+
public void testSingleCharacter() {
22+
assertEquals(0, BoothsAlgorithm.minimalRotationIndex("x"));
23+
}
24+
25+
@Test
26+
public void testRepeatedCharacters() {
27+
assertEquals(0, BoothsAlgorithm.minimalRotationIndex("aaaaa"));
28+
}
29+
30+
@Test
31+
public void testPeriodicString() {
32+
assertEquals(1, BoothsAlgorithm.minimalRotationIndex("baba"));
33+
assertEquals(1, BoothsAlgorithm.minimalRotationIndex("cabcab"));
34+
}
35+
36+
@Test
37+
public void testStringAlreadyHasMinimalRotationAtStart() {
38+
assertEquals(0, BoothsAlgorithm.minimalRotationIndex("abc"));
39+
}
40+
41+
@Test
42+
public void testNullInput() {
43+
assertThrows(IllegalArgumentException.class, () -> BoothsAlgorithm.minimalRotationIndex(null));
44+
}
45+
}

0 commit comments

Comments
 (0)