mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
style: enable HideUtilityClassConstructor
in checkstyle (#5147)
This commit is contained in:
@ -8,7 +8,9 @@ if it is smaller than the target, the rows above that element are ignored (becau
|
||||
above it will also be smaller than the target), else that element is greater than the target, then
|
||||
the rows below it are ignored.
|
||||
*/
|
||||
public class BinarySearch2dArray {
|
||||
public final class BinarySearch2dArray {
|
||||
private BinarySearch2dArray() {
|
||||
}
|
||||
|
||||
static int[] BinarySearch(int[][] arr, int target) {
|
||||
int rowCount = arr.length, colCount = arr[0].length;
|
||||
|
@ -25,7 +25,9 @@ import java.util.Scanner;
|
||||
1. [1,2,3,4] Number of rotations: 0 or 4(Both valid)
|
||||
2. [15,17,2,3,5] Number of rotations: 3
|
||||
*/
|
||||
class HowManyTimesRotated {
|
||||
final class HowManyTimesRotated {
|
||||
private HowManyTimesRotated() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
@ -2,7 +2,9 @@ package com.thealgorithms.searches;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class LinearSearchThread {
|
||||
public final class LinearSearchThread {
|
||||
private LinearSearchThread() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] list = new int[200];
|
||||
|
@ -1,46 +1,48 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
// URL: https://www.geeksforgeeks.org/order-agnostic-binary-search/
|
||||
|
||||
/* Order Agnostic Binary Search is an algorithm where we do not know whether the given
|
||||
sorted array is ascending or descending order.
|
||||
We declare a boolean variable to find whether the array is ascending order.
|
||||
In the while loop, we use the two pointer method (start and end) to get the middle element.
|
||||
if the middle element is equal to our target element, then that is the answer.
|
||||
If not, then we check if the array is ascending or descending order.
|
||||
Depending upon the condition, respective statements will be executed and we will get our answer.
|
||||
*/
|
||||
|
||||
public class OrderAgnosticBinarySearch {
|
||||
|
||||
static int BinSearchAlgo(int[] arr, int start, int end, int target) {
|
||||
|
||||
// Checking whether the given array is ascending order
|
||||
boolean AscOrd = arr[start] < arr[end];
|
||||
|
||||
while (start <= end) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Descending order
|
||||
else {
|
||||
if (arr[middle] > target)
|
||||
start = middle + 1;
|
||||
else
|
||||
end = middle - 1;
|
||||
}
|
||||
}
|
||||
// Element is not present
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
// URL: https://www.geeksforgeeks.org/order-agnostic-binary-search/
|
||||
|
||||
/* Order Agnostic Binary Search is an algorithm where we do not know whether the given
|
||||
sorted array is ascending or descending order.
|
||||
We declare a boolean variable to find whether the array is ascending order.
|
||||
In the while loop, we use the two pointer method (start and end) to get the middle element.
|
||||
if the middle element is equal to our target element, then that is the answer.
|
||||
If not, then we check if the array is ascending or descending order.
|
||||
Depending upon the condition, respective statements will be executed and we will get our answer.
|
||||
*/
|
||||
|
||||
public final class OrderAgnosticBinarySearch {
|
||||
private OrderAgnosticBinarySearch() {
|
||||
}
|
||||
|
||||
static int BinSearchAlgo(int[] arr, int start, int end, int target) {
|
||||
|
||||
// Checking whether the given array is ascending order
|
||||
boolean AscOrd = arr[start] < arr[end];
|
||||
|
||||
while (start <= end) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Descending order
|
||||
else {
|
||||
if (arr[middle] > target)
|
||||
start = middle + 1;
|
||||
else
|
||||
end = middle - 1;
|
||||
}
|
||||
}
|
||||
// Element is not present
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import java.util.Objects;
|
||||
* <a href="https://en.wikipedia.org/wiki/Median_of_medians">here</a>.
|
||||
*/
|
||||
public final class QuickSelect {
|
||||
private QuickSelect() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the {@code n}-th largest element of {@code list}, i.e. the element that would
|
||||
|
@ -16,7 +16,9 @@ import java.util.Scanner;
|
||||
*
|
||||
* @author Nishita Aggarwal
|
||||
*/
|
||||
public class SaddlebackSearch {
|
||||
public final class SaddlebackSearch {
|
||||
private SaddlebackSearch() {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method performs Saddleback Search
|
||||
|
@ -14,7 +14,9 @@ import java.util.Scanner;
|
||||
*
|
||||
* @author sahil
|
||||
*/
|
||||
public class SquareRootBinarySearch {
|
||||
public final class SquareRootBinarySearch {
|
||||
private SquareRootBinarySearch() {
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the driver method.
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.thealgorithms.searches;
|
||||
public class sortOrderAgnosticBinarySearch {
|
||||
public final class sortOrderAgnosticBinarySearch {
|
||||
private sortOrderAgnosticBinarySearch() {
|
||||
}
|
||||
public static int find(int[] arr, int key) {
|
||||
int start = 0;
|
||||
int end = arr.length - 1;
|
||||
|
Reference in New Issue
Block a user