Remove unnecessary code (#4141)

This commit is contained in:
Saurabh Rahate 2023-04-03 20:05:59 +05:30 committed by GitHub
parent 805f09850c
commit ad72c28d91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
64 changed files with 125 additions and 322 deletions

View File

@ -1079,7 +1079,7 @@ public class Blowfish {
*/
private String hexToBin(String hex) {
String binary = "";
Long num;
long num;
String binary4B;
int n = hex.length();
for (int i = 0; i < n; i++) {

View File

@ -160,7 +160,6 @@ public class HillCipher {
System.out.println(
"Invalid key, as determinant = 0. Program Terminated"
);
return;
}
}

View File

@ -175,7 +175,7 @@ public class AnyBaseToAnyBase {
// If the remainder is a digit < 10, simply add it to
// the left side of the new number.
if (decimalValue % b2 < 10) {
output = Integer.toString(decimalValue % b2) + output;
output = decimalValue % b2 + output;
} // If the remainder is >= 10, add a character with the
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
else {

View File

@ -34,8 +34,7 @@ public class OctalToDecimal {
public static int convertOctalToDecimal(String inputOctal) {
try {
// Actual conversion of Octal to Decimal:
Integer outputDecimal = Integer.parseInt(inputOctal, 8);
return outputDecimal;
return Integer.parseInt(inputOctal, 8);
} catch (NumberFormatException ne) {
// Printing a warning message if the input is not a valid octal
// number:

View File

@ -74,7 +74,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
for (i = 0; i < v - 1; i++) {
for (j = 0; j < e; j++) {
if (
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
dist[arr[j].u] != Integer.MAX_VALUE &&
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
) {
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
@ -85,7 +85,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
// Final cycle for negative checking
for (j = 0; j < e; j++) {
if (
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
dist[arr[j].u] != Integer.MAX_VALUE &&
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
) {
neg = 1;

View File

@ -28,7 +28,7 @@ public class BipartiteGrapfDFS {
for (Integer it : adj.get(node)) {
if (color[it] == -1) {
color[it] = 1 - color[node];
if (bipartite(V, adj, color, it) == false) {
if (!bipartite(V, adj, color, it)) {
return false;
}
} else if (color[it] == color[node]) {

View File

@ -12,7 +12,7 @@ class dijkstras {
int min = Integer.MAX_VALUE, min_index = -1;
for (int r = 0; r < k; r++) {
if (Set[r] == false && dist[r] <= min) {
if (!Set[r] && dist[r] <= min) {
min = dist[r];
min_index = r;
}

View File

@ -154,11 +154,7 @@ class AdjacencyMatrixGraph {
* @return whether or not the vertex exists
*/
public boolean vertexDoesExist(int aVertex) {
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
return true;
} else {
return false;
}
return aVertex >= 0 && aVertex < this.numberOfVertices();
}
/**
@ -343,14 +339,14 @@ class AdjacencyMatrixGraph {
public String toString() {
String s = " ";
for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " ";
s = s + i + " ";
}
s = s + " \n";
for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " : ";
s = s + i + " : ";
for (int j = 0; j < this.numberOfVertices(); j++) {
s = s + String.valueOf(this._adjacency[i][j]) + " ";
s = s + this._adjacency[i][j] + " ";
}
s = s + "\n";
}

View File

@ -17,7 +17,7 @@ class PrimMST {
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++) {
if (mstSet[v] == false && key[v] < min) {
if (!mstSet[v] && key[v] < min) {
min = key[v];
min_index = v;
}
@ -80,7 +80,7 @@ class PrimMST {
{
if (
graph[u][v] != 0 &&
mstSet[v] == false &&
!mstSet[v] &&
graph[u][v] < key[v]
) {
parent[v] = u;

View File

@ -114,7 +114,7 @@ public class TarjansAlgorithm {
stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph);
//update lowTime for the current node comparing lowtime of adj node
lowTime[u] = Math.min(lowTime[u], lowTime[n]);
} else if (isInStack[n] == true) {
} else if (isInStack[n]) {
//If adj node is in stack, update low
lowTime[u] = Math.min(lowTime[u], insertionTime[n]);
}

View File

@ -234,7 +234,6 @@ public class FibonacciHeap {
if (!curr.isMarked()) { //stop the recursion
curr.mark();
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
return;
} else {
if (curr.isRoot()) {
return;

View File

@ -51,18 +51,12 @@ public class MinPriorityQueue {
// returns boolean value whether the heap is empty or not
public boolean isEmpty() {
if (0 == this.size) {
return true;
}
return false;
return 0 == this.size;
}
// returns boolean value whether the heap is full or not
public boolean isFull() {
if (this.size == this.capacity) {
return true;
}
return false;
return this.size == this.capacity;
}
// prints the heap

View File

@ -174,8 +174,7 @@ public class CursorLinkedList<T> {
}
// 2- make the os point to the next of the @var{availableNodeIndex}
int availableNext = cursorSpace[availableNodeIndex].next;
cursorSpace[os].next = availableNext;
cursorSpace[os].next = cursorSpace[availableNodeIndex].next;
// this to indicate an end of the list , helpful at testing since any err
// would throw an outOfBoundException

View File

@ -407,7 +407,7 @@ public class SinglyLinkedList extends Node {
list.insert(3);
list.insertNth(1, 4);
assert list.toString().equals("10->7->5->3->1");
System.out.println(list.toString());
System.out.println(list);
/* Test search function */
assert list.search(10) &&
list.search(5) &&
@ -424,7 +424,7 @@ public class SinglyLinkedList extends Node {
list.deleteNth(1);
list.delete();
assert list.toString().equals("7->3");
System.out.println(list.toString());
System.out.println(list);
assert list.size == 2 && list.size() == list.count();
list.clear();

View File

@ -17,21 +17,13 @@ public class CircularQueue {
}
public boolean isEmpty() {
if (beginningOfQueue == -1) {
return true;
} else {
return false;
}
return beginningOfQueue == -1;
}
public boolean isFull() {
if (topOfQueue + 1 == beginningOfQueue) {
return true;
} else if (topOfQueue == size - 1 && beginningOfQueue == 0) {
return true;
} else {
return false;
}
} else return topOfQueue == size - 1 && beginningOfQueue == 0;
}
public void enQueue(int value) {

View File

@ -91,13 +91,12 @@ public class Deques<T> {
if (tail == null) {
// If the deque is empty, add the node as the head and tail
head = newNode;
tail = newNode;
} else {
// If the deque is not empty, insert the node as the new tail
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
tail = newNode;
size++;
}

View File

@ -177,6 +177,6 @@ public class Queues {
System.out.println(myQueue.peekFront()); // Will print 2
System.out.println(myQueue.peekRear()); // Will print 7
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
System.out.println(myQueue); // Will print [2, 5, 3, 7]
}
}

View File

@ -49,8 +49,7 @@ public class AVLSimple {
private Node insert(Node node, int item) {
if (node == null) {
Node add = new Node(item);
return add;
return new Node(item);
}
if (node.data > item) {
node.left = insert(node.left, item);

View File

@ -62,22 +62,21 @@ public class AVLTree {
}
return;
}
Node child;
if (node.left != null) {
Node child = node.left;
child = node.left;
while (child.right != null) {
child = child.right;
}
node.key = child.key;
delete(child);
} else {
Node child = node.right;
child = node.right;
while (child.left != null) {
child = child.left;
}
}
node.key = child.key;
delete(child);
}
}
public void delete(int delKey) {
if (root == null) {
@ -216,11 +215,7 @@ public class AVLTree {
public boolean search(int key) {
Node result = searchHelper(this.root, key);
if (result != null) {
return true;
}
return false;
return result != null;
}
private Node searchHelper(Node root, int key) {

View File

@ -117,11 +117,9 @@ public class BinaryTree {
if (value < parent.data) {
parent.left = newNode;
parent.left.parent = parent;
return;
} else {
parent.right = newNode;
parent.right.parent = parent;
return;
}
}
}
@ -177,7 +175,6 @@ public class BinaryTree {
if (temp == root) {
successor.parent = null;
root = successor;
return true;
} // If you're not deleting the root
else {
successor.parent = temp.parent;
@ -188,8 +185,8 @@ public class BinaryTree {
} else {
temp.parent.left = successor;
}
return true;
}
return true;
} // One child
else {
// If it has a right child
@ -207,7 +204,6 @@ public class BinaryTree {
} else {
temp.parent.right = temp.right;
}
return true;
} // If it has a left child
else {
if (temp == root) {
@ -223,8 +219,8 @@ public class BinaryTree {
} else {
temp.parent.right = temp.left;
}
return true;
}
return true;
}
}

View File

@ -168,7 +168,6 @@ public class GenericTree {
for (int i = 0; i < node.child.size(); i++) {
depth(node.child.get(i), dep - 1);
}
return;
}
/**

View File

@ -309,7 +309,6 @@ public class RedBlackBST {
public void insertDemo() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Add items");
int item;
@ -324,8 +323,6 @@ public class RedBlackBST {
printTree(root);
System.out.println("Pre order");
printTreepre(root);
break;
}
scan.close();
}

View File

@ -62,7 +62,7 @@ public class TrieImp {
}
currentNode = node;
}
if (currentNode.end == true) {
if (currentNode.end) {
currentNode.end = false;
return true;
}

View File

@ -3,13 +3,6 @@ package com.thealgorithms.dynamicprogramming;
/* A Naive recursive implementation
of 0-1 Knapsack problem */
public class BruteForceKnapsack {
// A utility function that returns
// maximum of two integers
static int max(int a, int b) {
return (a > b) ? a : b;
}
// Returns the maximum value that
// can be put in a knapsack of
// capacity W
@ -29,8 +22,7 @@ public class BruteForceKnapsack {
// (1) nth item included
// (2) not included
else {
return max(
val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
return Math.max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1)
);
}

View File

@ -3,11 +3,6 @@ package com.thealgorithms.dynamicprogramming;
// A Dynamic Programming based solution
// for 0-1 Knapsack problem
public class DyanamicProgrammingKnapsack {
static int max(int a, int b) {
return (a > b) ? a : b;
}
// Returns the maximum value that can
// be put in a knapsack of capacity W
static int knapSack(int W, int wt[], int val[], int n) {
@ -20,8 +15,7 @@ public class DyanamicProgrammingKnapsack {
if (i == 0 || w == 0) {
K[i][w] = 0;
} else if (wt[i - 1] <= w) {
K[i][w] =
max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
K[i][w] = Math.max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
} else {
K[i][w] = K[i - 1][w];
}

View File

@ -57,8 +57,8 @@ public class EditDistance {
int insert = dp[i][j + 1] + 1;
int delete = dp[i + 1][j] + 1;
int min = replace > insert ? insert : replace;
min = delete > min ? min : delete;
int min = Math.min(replace, insert);
min = Math.min(delete, min);
dp[i + 1][j + 1] = min;
}
}
@ -110,13 +110,12 @@ public class EditDistance {
if (s1.charAt(0) == s2.charAt(0)) {
storage[m][n] =
editDistance(s1.substring(1), s2.substring(1), storage);
return storage[m][n];
} else {
int op1 = editDistance(s1, s2.substring(1), storage);
int op2 = editDistance(s1.substring(1), s2, storage);
int op3 = editDistance(s1.substring(1), s2.substring(1), storage);
storage[m][n] = 1 + Math.min(op1, Math.min(op2, op3));
}
return storage[m][n];
}
}
}

View File

@ -30,10 +30,7 @@ class LongestCommonSubsequence {
if (arr1[i - 1].equals(arr2[j - 1])) {
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
} else {
lcsMatrix[i][j] =
lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]
? lcsMatrix[i - 1][j]
: lcsMatrix[i][j - 1];
lcsMatrix[i][j] = Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]);
}
}
}

View File

@ -27,19 +27,9 @@ public class LongestPalindromicSubstring {
if (g == 0) {
arr[i][j] = true;
} else if (g == 1) {
if (input.charAt(i) == input.charAt(j)) {
arr[i][j] = true;
arr[i][j] = input.charAt(i) == input.charAt(j);
} else {
arr[i][j] = false;
}
} else {
if (
input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1]
) {
arr[i][j] = true;
} else {
arr[i][j] = false;
}
arr[i][j] = input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1];
}
if (arr[i][j]) {

View File

@ -87,7 +87,7 @@ public class MatrixChainMultiplication {
private static void printArray(int[][] array) {
for (int i = 1; i < size + 1; i++) {
for (int j = 1; j < size + 1; j++) {
System.out.print(String.format("%7d", array[i][j]));
System.out.printf("%7d", array[i][j]);
}
System.out.println();
}

View File

@ -48,27 +48,21 @@ public class PalindromicPartitioning {
if (L == 2) {
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j));
} else {
if (
(word.charAt(i) == word.charAt(j)) &&
isPalindrome[i + 1][j - 1]
) {
isPalindrome[i][j] = true;
} else {
isPalindrome[i][j] = false;
}
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j)) &&
isPalindrome[i + 1][j - 1];
}
}
}
//We find the minimum for each index
for (i = 0; i < len; i++) {
if (isPalindrome[0][i] == true) {
if (isPalindrome[0][i]) {
minCuts[i] = 0;
} else {
minCuts[i] = Integer.MAX_VALUE;
for (j = 0; j < i; j++) {
if (
isPalindrome[j + 1][i] == true &&
isPalindrome[j + 1][i] &&
1 + minCuts[j] < minCuts[i]
) {
minCuts[i] = 1 + minCuts[j];

View File

@ -112,7 +112,7 @@ public class RegexMatching {
return true;
}
if (strg[svidx][pvidx] != 0) {
return strg[svidx][pvidx] == 1 ? false : true;
return strg[svidx][pvidx] != 1;
}
char chs = src.charAt(svidx);
char chp = pat.charAt(pvidx);
@ -127,7 +127,7 @@ public class RegexMatching {
} else {
ans = false;
}
strg[svidx][pvidx] = ans == false ? 1 : 2;
strg[svidx][pvidx] = ans ? 2 : 1;
return ans;
}

View File

@ -24,9 +24,7 @@ public class WineProblem {
int start = WPRecursion(arr, si + 1, ei) + arr[si] * year;
int end = WPRecursion(arr, si, ei - 1) + arr[ei] * year;
int ans = Math.max(start, end);
return ans;
return Math.max(start, end);
}
// Method 2: Top-Down DP(Memoization)

View File

@ -58,7 +58,7 @@ public class AmicableNumber {
countofRes +
" Amicable_numbers.These are \n "
);
System.out.println(res.toString());
System.out.println(res);
}
/**

View File

@ -52,8 +52,7 @@ public class Combinations {
// nC0 is always 1
long solution = 1;
for (int i = 0; i < k; i++) {
long next = (n - i) * solution / (i + 1);
solution = next;
solution = (n - i) * solution / (i + 1);
}
return solution;
}

View File

@ -10,8 +10,7 @@ public class DistanceFormula {
) {
double dX = Math.pow(x2 - x1, 2);
double dY = Math.pow(y2 - x1, 2);
double d = Math.sqrt(dX + dY);
return d;
return Math.sqrt(dX + dY);
}
public static double manhattanDistance(
@ -20,8 +19,7 @@ public class DistanceFormula {
double x2,
double y2
) {
double d = Math.abs(x1 - x2) + Math.abs(y1 - y2);
return d;
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
public static int hammingDistance(int[] b1, int[] b2) {

View File

@ -26,22 +26,14 @@ public class EulerMethod {
BiFunction<Double, Double, Double> exampleEquation1 = (x, y) -> x;
ArrayList<double[]> points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1);
assert points1.get(points1.size() - 1)[1] == 7.800000000000003;
points1.forEach(point ->
System.out.println(
String.format("x: %1$f; y: %2$f", point[0], point[1])
)
);
points1.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
// example from https://en.wikipedia.org/wiki/Euler_method
System.out.println("\n\nexample 2:");
BiFunction<Double, Double, Double> exampleEquation2 = (x, y) -> y;
ArrayList<double[]> points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2);
assert points2.get(points2.size() - 1)[1] == 45.25925556817596;
points2.forEach(point ->
System.out.println(
String.format("x: %1$f; y: %2$f", point[0], point[1])
)
);
points2.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
System.out.println("\n\nexample 3:");
@ -55,11 +47,7 @@ public class EulerMethod {
exampleEquation3
);
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
points3.forEach(point ->
System.out.println(
String.format("x: %1$f; y: %2$f", point[0], point[1])
)
);
points3.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
}
/**
@ -83,11 +71,7 @@ public class EulerMethod {
"stepSize should be greater than zero"
);
}
double yNext =
yCurrent +
stepSize *
differentialEquation.apply(xCurrent, yCurrent);
return yNext;
return yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent);
}
/**

View File

@ -17,7 +17,7 @@ public class FastInverseSqrt {
i = 0x5f3759df - (i >> 1);
x = Float.intBitsToFloat(i);
x = x * (1.5f - xhalf * x * x);
return x == (float) ((float) 1 / (float) Math.sqrt(number));
return x == ((float) 1 / (float) Math.sqrt(number));
}
/**

View File

@ -36,11 +36,7 @@ public class KrishnamurthyNumber {
}
//evaluating if sum of the factorials of the digits equals the number itself
if (tmp == s) {
return true;
} else {
return false;
}
return tmp == s;
}
}

View File

@ -3,7 +3,6 @@ package com.thealgorithms.maths;
public class StandardScore {
public static double zScore(double num, double mean, double stdDev) {
double z = (num - mean) / stdDev;
return z;
return (num - mean) / stdDev;
}
}

View File

@ -88,7 +88,7 @@ public class BankersAlgorithm {
while (count < totalProcess) {
boolean foundSafeSystem = false;
for (int m = 0; m < totalProcess; m++) {
if (finishProcesses[m] == false) {
if (!finishProcesses[m]) {
int j;
for (j = 0; j < totalResources; j++) {
@ -112,7 +112,7 @@ public class BankersAlgorithm {
}
// If we could not find a next process in safe sequence.
if (foundSafeSystem == false) {
if (!foundSafeSystem) {
System.out.print(
"The system is not in the safe state because lack of resources"
);

View File

@ -131,15 +131,7 @@ class Graph {
) {
return false;
}
if (
neighbours != null
? !neighbours.equals(vertex.neighbours)
: vertex.neighbours != null
) {
return false;
}
return true;
return neighbours != null ? neighbours.equals(vertex.neighbours) : vertex.neighbours == null;
}
@Override

View File

@ -120,7 +120,7 @@ class Trieac {
}
// If prefix is present as a word.
boolean isWord = (pCrawl.isWordEnd == true);
boolean isWord = (pCrawl.isWordEnd);
// If prefix is last node of tree (has no
// children)

View File

@ -139,7 +139,7 @@ public class LowestBasePalindrome {
// If the remainder is a digit < 10, simply add it to
// the left side of the new number.
if (decimalValue % b2 < 10) {
output = Integer.toString(decimalValue % b2) + output;
output = decimalValue % b2 + output;
} // If the remainder is >= 10, add a character with the
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
else {

View File

@ -46,7 +46,7 @@ public class MiniMaxAlgorithm {
"The best score for " +
(isMaximizer ? "Maximizer" : "Minimizer") +
" is " +
String.valueOf(bestScore)
bestScore
);
}
@ -88,14 +88,12 @@ public class MiniMaxAlgorithm {
// (1 x 2) = 2; ((1 x 2) + 1) = 3
// (2 x 2) = 4; ((2 x 2) + 1) = 5 ...
if (verbose) {
System.out.println(
String.format(
"From %02d and %02d, %s chooses %02d",
System.out.printf(
"From %02d and %02d, %s chooses %02d%n",
score1,
score2,
(isMaximizer ? "Maximizer" : "Minimizer"),
bestScore
)
);
}

View File

@ -49,7 +49,7 @@ class PageRank {
for (k = 1; k <= totalNodes; k++) {
this.pagerank[k] = InitialPageRank;
}
System.out.printf("\n Initial PageRank Values , 0th Step \n");
System.out.print("\n Initial PageRank Values , 0th Step \n");
for (k = 1; k <= totalNodes; k++) {
System.out.printf(
@ -113,7 +113,7 @@ class PageRank {
}
// Display PageRank
System.out.printf("\n Final Page Rank : \n");
System.out.print("\n Final Page Rank : \n");
for (k = 1; k <= totalNodes; k++) {
System.out.printf(
" Page Rank of " + k + " is :\t" + this.pagerank[k] + "\n"

View File

@ -40,7 +40,7 @@ public class RemoveDuplicateFromString {
for (int i = 0; i < n; i++) {
if (sb.toString().indexOf(s.charAt(i)) == -1) {
sb.append(String.valueOf(s.charAt(i)));
sb.append(s.charAt(i));
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
@ -86,23 +84,15 @@ class BinarySearch implements SearchAlgorithm {
BinarySearch search = new BinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
@ -29,24 +27,16 @@ class ExponentialSearch implements SearchAlgorithm {
ExponentialSearch search = new ExponentialSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
@Override

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;
@ -67,28 +65,20 @@ class InterpolationSearch {
.toArray();
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
int shouldBeFound = integers[r.nextInt(size - 1)];
InterpolationSearch search = new InterpolationSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
String.format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
@ -72,23 +70,15 @@ public final class IterativeBinarySearch implements SearchAlgorithm {
IterativeBinarySearch search = new IterativeBinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
String.format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
@ -70,23 +68,15 @@ public class IterativeTernarySearch implements SearchAlgorithm {
IterativeTernarySearch search = new IterativeTernarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@ -53,14 +53,12 @@ public class LinearSearch implements SearchAlgorithm {
LinearSearch search = new LinearSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
String.format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
@ -48,24 +46,16 @@ class LowerBound implements SearchAlgorithm {
LowerBound search = new LowerBound();
int atIndex = search.find(integers, val);
System.out.println(
format(
"Val: %d. Lower Bound Found %d at index %d. An array length %d",
System.out.printf(
"Val: %d. Lower Bound Found %d at index %d. An array length %d%n",
val,
integers[atIndex],
atIndex,
size
)
);
boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val;
System.out.println(
format(
"Lower Bound found at an index: %d. Is greater or max element: %b",
atIndex,
toCheck
)
);
System.out.printf("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
}
/**

View File

@ -187,13 +187,11 @@ public class MonteCarloTreeSearch {
System.out.println("N.\tScore\t\tVisits");
for (int i = 0; i < rootNode.childNodes.size(); i++) {
System.out.println(
String.format(
"%02d\t%d\t\t%d",
System.out.printf(
"%02d\t%d\t\t%d%n",
i + 1,
rootNode.childNodes.get(i).score,
rootNode.childNodes.get(i).visitCount
)
);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
@ -89,23 +87,15 @@ public class TernarySearch implements SearchAlgorithm {
TernarySearch search = new TernarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
format(
"Should be found: %d. Found %d at index %d. An array length %d",
System.out.printf(
"Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound,
integers[atIndex],
atIndex,
size
)
);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(
format(
"Found by system method at an index: %d. Is equal: %b",
toCheck,
toCheck == atIndex
)
);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches;
import static java.lang.String.format;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
@ -48,24 +46,16 @@ class UpperBound implements SearchAlgorithm {
UpperBound search = new UpperBound();
int atIndex = search.find(integers, val);
System.out.println(
format(
"Val: %d. Upper Bound Found %d at index %d. An array length %d",
System.out.printf(
"Val: %d. Upper Bound Found %d at index %d. An array length %d%n",
val,
integers[atIndex],
atIndex,
size
)
);
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
System.out.println(
format(
"Upper Bound found at an index: %d. Is greater or max element: %b",
atIndex,
toCheck
)
);
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
}
/**

View File

@ -27,7 +27,7 @@ public class BeadSort {
for(int i = 0; i < unsorted.length; i++) {
int k = 0;
for(int j = 0; j < (int) unsorted[i] ; j++) {
for(int j = 0; j < unsorted[i]; j++) {
grid[count[max - k - 1]++][k] = '*';
k++;
}

View File

@ -24,7 +24,7 @@ public class CircleSort implements SortAlgorithm {
int left,
int right
) {
Boolean swapped = false;
boolean swapped = false;
if (left == right) {
return false;

View File

@ -23,7 +23,7 @@ class CombSort implements SortAlgorithm {
private int nextGap(int gap) {
// Shrink gap by Shrink factor
gap = (gap * 10) / 13;
return (gap < 1) ? 1 : gap;
return Math.max(gap, 1);
}
/**

View File

@ -13,8 +13,7 @@ public class MergeSortRecursive {
}
public List<Integer> mergeSort() {
List<Integer> arrSorted = merge(arr);
return arrSorted;
return merge(arr);
}
private static List<Integer> merge(List<Integer> arr) {

View File

@ -72,20 +72,20 @@ public class TreeSort implements SortAlgorithm {
// ==== Integer Array =======
System.out.println("Testing for Integer Array....");
Integer[] a = { 3, -7, 45, 1, 343, -5, 2, 9 };
System.out.print(String.format("%-10s", "unsorted: "));
System.out.printf("%-10s", "unsorted: ");
print(a);
a = treeSort.sort(a);
System.out.print(String.format("%-10s", "sorted: "));
System.out.printf("%-10s", "sorted: ");
print(a);
System.out.println();
// ==== Integer List =======
System.out.println("Testing for Integer List....");
List<Integer> intList = List.of(3, -7, 45, 1, 343, -5, 2, 9);
System.out.print(String.format("%-10s", "unsorted: "));
System.out.printf("%-10s", "unsorted: ");
print(intList);
intList = treeSort.sort(intList);
System.out.print(String.format("%-10s", "sorted: "));
System.out.printf("%-10s", "sorted: ");
print(intList);
System.out.println();
@ -101,10 +101,10 @@ public class TreeSort implements SortAlgorithm {
"apple",
"pineapple",
};
System.out.print(String.format("%-10s", "unsorted: "));
System.out.printf("%-10s", "unsorted: ");
print(b);
b = treeSort.sort(b);
System.out.print(String.format("%-10s", "sorted: "));
System.out.printf("%-10s", "sorted: ");
print(b);
System.out.println();
@ -120,10 +120,10 @@ public class TreeSort implements SortAlgorithm {
"apple",
"pineapple"
);
System.out.print(String.format("%-10s", "unsorted: "));
System.out.printf("%-10s", "unsorted: ");
print(stringList);
stringList = treeSort.sort(stringList);
System.out.print(String.format("%-10s", "sorted: "));
System.out.printf("%-10s", "sorted: ");
print(stringList);
}
}

View File

@ -59,7 +59,7 @@ public class WiggleSort implements SortAlgorithm {
median =
select(
Arrays.<T>asList(sortThis),
Arrays.asList(sortThis),
(int) floor(sortThis.length / 2.0)
);

View File

@ -27,7 +27,7 @@ class Solution {
String maxStr = "";
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
if (isValid(s, i, j) == true) {
if (isValid(s, i, j)) {
if (j - i + 1 > maxStr.length()) { // update maxStr
maxStr = s.substring(i, j + 1);
}

View File

@ -72,11 +72,6 @@ public static int myAtoi(String s) {
if (db1 > (2147483647)) {
return 2147483647;
}
}else if (number.length() == 10 && negative) {
double db1 = Double.parseDouble(number);
if (db1 >= 2147483648d) {
return -2147483648;
}
}
if(negative){