style: enable NeedBraces in checkstyle (#5227)

* enable style NeedBraces

* style: enable NeedBraces in checkstyle

---------

Co-authored-by: Samuel Facchinello <samuel.facchinello@piksel.com>
This commit is contained in:
Samuel Facchinello
2024-06-13 21:00:16 +02:00
committed by GitHub
parent 51fcc66345
commit 87b17e0571
68 changed files with 629 additions and 261 deletions

View File

@ -29,47 +29,57 @@ public final class BinarySearch2dArray {
if (arr[midRow][midCol] == target) {
return new int[] {midRow, midCol};
} else if (arr[midRow][midCol] < target)
} else if (arr[midRow][midCol] < target) {
startRow = midRow;
else
} else {
endRow = midRow;
}
}
/*
if the above search fails to find the target element, these conditions will be used to
find the target element, which further uses the binary search algorithm in the places
which were left unexplored.
*/
if (arr[startRow][midCol] == target)
if (arr[startRow][midCol] == target) {
return new int[] {
startRow,
midCol,
};
}
if (arr[endRow][midCol] == target) return new int[] {endRow, midCol};
if (arr[endRow][midCol] == target) {
return new int[] {endRow, midCol};
}
if (target <= arr[startRow][midCol - 1]) return binarySearch(arr, target, startRow, 0, midCol - 1);
if (target <= arr[startRow][midCol - 1]) {
return binarySearch(arr, target, startRow, 0, midCol - 1);
}
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1]) return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1]) {
return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
}
if (target <= arr[endRow][midCol - 1])
if (target <= arr[endRow][midCol - 1]) {
return binarySearch(arr, target, endRow, 0, midCol - 1);
else
} else {
return binarySearch(arr, target, endRow, midCol + 1, colCount - 1);
}
}
static int[] binarySearch(int[][] arr, int target, int row, int colStart, int colEnd) {
while (colStart <= colEnd) {
int midIndex = colStart + (colEnd - colStart) / 2;
if (arr[row][midIndex] == target)
if (arr[row][midIndex] == target) {
return new int[] {
row,
midIndex,
};
else if (arr[row][midIndex] < target)
} else if (arr[row][midIndex] < target) {
colStart = midIndex + 1;
else
} else {
colEnd = midIndex - 1;
}
}
return new int[] {-1, -1};

View File

@ -32,10 +32,11 @@ class KMPSearch {
else if (i < n && pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
if (j != 0) {
j = lps[j - 1];
else
} else {
i = i + 1;
}
}
}
System.out.println("No pattern found");

View File

@ -24,22 +24,23 @@ public final class OrderAgnosticBinarySearch {
int middle = start + (end - start) / 2;
// Check if the desired element is present at the middle position
if (arr[middle] == target) return middle; // returns the index of the middle element
// Ascending order
if (ascOrd) {
if (arr[middle] < target)
start = middle + 1;
else
end = middle - 1;
if (arr[middle] == target) {
return middle; // returns the index of the middle element
}
// Descending order
else {
if (arr[middle] > target)
if (ascOrd) {
// Ascending order
if (arr[middle] < target) {
start = middle + 1;
else
} else {
end = middle - 1;
}
} else {
// Descending order
if (arr[middle] > target) {
start = middle + 1;
} else {
end = middle - 1;
}
}
}
// Element is not present

View File

@ -56,7 +56,9 @@ public final class QuickSelect {
private static <T extends Comparable<T>> int selectIndex(List<T> list, int left, int right, int n) {
while (true) {
if (left == right) return left;
if (left == right) {
return left;
}
int pivotIndex = pivot(list, left, right);
pivotIndex = partition(list, left, right, pivotIndex, n);
if (n == pivotIndex) {

View File

@ -18,7 +18,9 @@ public final class RabinKarpAlgorithm {
int h = 1;
// The value of h would be "pow(d, patternLength-1)%primeNumber"
for (int i = 0; i < patternLength - 1; i++) h = (h * ALPHABET_SIZE) % primeNumber;
for (int i = 0; i < patternLength - 1; i++) {
h = (h * ALPHABET_SIZE) % primeNumber;
}
// Calculate the hash value of pattern and first
// window of text
@ -37,7 +39,9 @@ public final class RabinKarpAlgorithm {
if (hashForPattern == hashForText) {
/* Check for characters one by one */
for (j = 0; j < patternLength; j++) {
if (text.charAt(i + j) != pattern.charAt(j)) break;
if (text.charAt(i + j) != pattern.charAt(j)) {
break;
}
}
// if hashForPattern == hashForText and pattern[0...patternLength-1] = text[i, i+1, ...i+patternLength-1]
@ -53,7 +57,9 @@ public final class RabinKarpAlgorithm {
hashForText = (ALPHABET_SIZE * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
// handling negative hashForText
if (hashForText < 0) hashForText = (hashForText + primeNumber);
if (hashForText < 0) {
hashForText = (hashForText + primeNumber);
}
}
}
return index; // return -1 if pattern does not found

View File

@ -67,10 +67,11 @@ public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgori
RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
int res = searcher.find(a, t);
if (res == -1)
if (res == -1) {
System.out.println("Element not found in the array.");
else
} else {
System.out.println("Element found at index " + res);
}
}
}
}