Skip to content

Commit ec19a52

Browse files
Merge branch 'master' into contrib/booths-minimal-rotation
2 parents f33ad1a + 862de93 commit ec19a52

19 files changed

Lines changed: 257 additions & 230 deletions

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v7
1212
- name: Set up JDK
13-
uses: actions/setup-java@v6.0.1
13+
uses: actions/setup-java@v6.1.0
1414
with:
1515
java-version: 21
1616
distribution: 'temurin'

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
uses: actions/checkout@v7
2525

2626
- name: Set up JDK
27-
uses: actions/setup-java@v6.0.1
27+
uses: actions/setup-java@v6.1.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.38.2
33+
uses: github/codeql-action/init@v4.39.0
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.38.2
41+
uses: github/codeql-action/analyze@v4.39.0
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.38.2
58+
uses: github/codeql-action/init@v4.39.0
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.38.2
63+
uses: github/codeql-action/analyze@v4.39.0
6464
with:
6565
category: "/language:actions"
6666
...

‎.github/workflows/infer.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v7
1919

2020
- name: Set up JDK
21-
uses: actions/setup-java@v6.0.1
21+
uses: actions/setup-java@v6.1.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ These implementations are intended for learning purposes. As such, they may be l
1717
Please read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute to this project.
1818

1919
## Algorithms
20-
Our [directory](DIRECTORY.md) has the full list of applications.
20+
Our [directory](DIRECTORY.md) contains the full list of algorithms implemented in Java.

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<plugin>
120120
<groupId>com.github.spotbugs</groupId>
121121
<artifactId>spotbugs-maven-plugin</artifactId>
122-
<version>4.10.4.1</version>
122+
<version>4.10.5.0</version>
123123
<configuration>
124124
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
125125
<includeTests>true</includeTests>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Note: This algorithm requires that the input array be sorted.
2020
* </p>
2121
*/
22-
class ExponentialSearch implements SearchAlgorithm {
22+
public final class ExponentialSearch implements SearchAlgorithm {
2323

2424
/**
2525
* Finds the index of the specified key in a sorted array using exponential search.
@@ -31,13 +31,13 @@ class ExponentialSearch implements SearchAlgorithm {
3131
*/
3232
@Override
3333
public <T extends Comparable<T>> int find(T[] array, T key) {
34-
if (array.length == 0) {
34+
if (array == null || array.length == 0 || key == null) {
3535
return -1;
3636
}
37-
if (array[0].equals(key)) {
37+
if (key.compareTo(array[0]) == 0) {
3838
return 0;
3939
}
40-
if (array[array.length - 1].equals(key)) {
40+
if (key.compareTo(array[array.length - 1]) == 0) {
4141
return array.length - 1;
4242
}
4343

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@
1515
* Note: This algorithm requires that the input array be sorted.
1616
* </p>
1717
*/
18-
@SuppressWarnings({"rawtypes", "unchecked"})
19-
public class FibonacciSearch implements SearchAlgorithm {
18+
public final class FibonacciSearch implements SearchAlgorithm {
2019

2120
/**
2221
* Finds the index of the specified key in a sorted array using Fibonacci search.
2322
*
2423
* @param array The sorted array to search.
2524
* @param key The element to search for.
2625
* @param <T> The type of the elements in the array, which must be comparable.
27-
* @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
26+
* @throws IllegalArgumentException if the input array is null, not sorted or empty, or if the key is null.
2827
* @return The index of the key if found, otherwise -1.
2928
*/
3029
@Override
3130
public <T extends Comparable<T>> int find(T[] array, T key) {
31+
if (array == null) {
32+
throw new IllegalArgumentException("Input array must not be null.");
33+
}
3234
if (array.length == 0) {
3335
throw new IllegalArgumentException("Input array must not be empty.");
3436
}
@@ -76,7 +78,7 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
7678
return -1;
7779
}
7880

79-
private boolean isSorted(Comparable[] array) {
81+
private <T extends Comparable<T>> boolean isSorted(T[] array) {
8082
for (int i = 1; i < array.length; i++) {
8183
if (array[i - 1].compareTo(array[i]) > 0) {
8284
return false;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public class LinearSearch implements SearchAlgorithm {
5454
*
5555
* @param array List to be searched (can be unsorted)
5656
* @param value Key being searched for
57-
* @return Location of the key, -1 if array is null or empty, or key not found
57+
* @return the index of the matching element, or -1 if the array is null or empty,
58+
* the value is null, or no matching element is found; null array elements are skipped
5859
*/
5960
@Override
6061
public <T extends Comparable<T>> int find(T[] array, T value) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* This is an extension of BinarySearch.
1414
*
1515
* <p>
16-
* Worst-case performance O(log n) Best-case performance O(1) Average
16+
* Worst-case performance O(log n) Best-case performance O(log n) Average
1717
* performance O(log n) Worst-case space complexity O(1)
1818
*
1919
* @author Pratik Padalia (https://github.com/15pratik)
@@ -26,10 +26,13 @@ class LowerBound implements SearchAlgorithm {
2626
* @param array is an array where the LowerBound value is to be found
2727
* @param key is an element for which the LowerBound is to be found
2828
* @param <T> is any comparable type
29-
* @return index of the LowerBound element
29+
* @return index of the first element not less than key, or 0 if array is null/empty or key is null
3030
*/
3131
@Override
3232
public <T extends Comparable<T>> int find(T[] array, T key) {
33+
if (array == null || array.length == 0 || key == null) {
34+
return 0;
35+
}
3336
return search(array, key, 0, array.length - 1);
3437
}
3538

@@ -51,10 +54,8 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
5154
int median = (left + right) >>> 1;
5255
int comp = key.compareTo(array[median]);
5356

54-
if (comp == 0) {
55-
return median;
56-
} else if (comp < 0) {
57-
// median position can be a possible solution
57+
if (comp <= 0) {
58+
// key is less than or equal to array[median], median position can be a possible solution
5859
return search(array, key, left, median);
5960
} else {
6061
// key we are looking is greater, so we must look on the right of median position

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @see LinearSearch
3434
* @see SearchAlgorithm
3535
*/
36-
public class SentinelLinearSearch implements SearchAlgorithm {
36+
public final class SentinelLinearSearch implements SearchAlgorithm {
3737
/**
3838
* Performs sentinel linear search on the given array.
3939
*
@@ -65,8 +65,8 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
6565

6666
int i = 0;
6767
// Search without bound checking since sentinel guarantees we'll find the key
68-
// Null check for array element to prevent NPE when array contains null elements
69-
while (array[i] != null && array[i].compareTo(key) != 0) {
68+
// Null check for array element to skip nulls and prevent NPE when array contains null elements
69+
while (array[i] == null || array[i].compareTo(key) != 0) {
7070
i++;
7171
}
7272

0 commit comments

Comments
 (0)