diff --git a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java index 424c451ed..8acaa954c 100644 --- a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java +++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java @@ -91,7 +91,7 @@ public class AllPathsFromSourceToTarget { } // Driver program - public static List> allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination) + public static List> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) { // Create a sample graph AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices); diff --git a/src/main/java/com/thealgorithms/ciphers/Blowfish.java b/src/main/java/com/thealgorithms/ciphers/Blowfish.java index b60cf7c7a..8864fc75f 100644 --- a/src/main/java/com/thealgorithms/ciphers/Blowfish.java +++ b/src/main/java/com/thealgorithms/ciphers/Blowfish.java @@ -11,7 +11,7 @@ package com.thealgorithms.ciphers; public class Blowfish { //Initializing substitution boxes - String S[][] = { + String[][] S = { { "d1310ba6", "98dfb5ac", @@ -1047,7 +1047,7 @@ public class Blowfish { }; //Initializing subkeys with digits of pi - String P[] = { + String[] P = { "243f6a88", "85a308d3", "13198a2e", @@ -1146,7 +1146,7 @@ public class Blowfish { The outputs are added modulo 232 and XORed to produce the final 32-bit output */ private String f(String plainText) { - String a[] = new String[4]; + String[] a = new String[4]; String ans = ""; for (int i = 0; i < 8; i += 2) { //column number for S-box is a 8-bit value diff --git a/src/main/java/com/thealgorithms/ciphers/HillCipher.java b/src/main/java/com/thealgorithms/ciphers/HillCipher.java index 102d760d1..ffc7e08be 100644 --- a/src/main/java/com/thealgorithms/ciphers/HillCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/HillCipher.java @@ -22,7 +22,7 @@ public class HillCipher { System.out.println("Enter key matrix size"); int matrixSize = userInput.nextInt(); System.out.println("Enter Key/encryptionKey matrix "); - int keyMatrix[][] = new int[matrixSize][matrixSize]; + int[][] keyMatrix = new int[matrixSize][matrixSize]; for (int i = 0; i < matrixSize; i++) { for (int j = 0; j < matrixSize; j++) { keyMatrix[i][j] = userInput.nextInt(); @@ -33,7 +33,7 @@ public class HillCipher { int[][] messageVector = new int[matrixSize][1]; String CipherText = ""; - int cipherMatrix[][] = new int[matrixSize][1]; + int[][] cipherMatrix = new int[matrixSize][1]; int j = 0; while (j < message.length()) { for (int i = 0; i < matrixSize; i++) { @@ -69,7 +69,7 @@ public class HillCipher { System.out.println("Enter key matrix size"); int n = userInput.nextInt(); System.out.println("Enter inverseKey/decryptionKey matrix "); - int keyMatrix[][] = new int[n][n]; + int[][] keyMatrix = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { keyMatrix[i][j] = userInput.nextInt(); @@ -81,7 +81,7 @@ public class HillCipher { //solving for the required plaintext message int[][] messageVector = new int[n][1]; String PlainText = ""; - int plainMatrix[][] = new int[n][1]; + int[][] plainMatrix = new int[n][1]; int j = 0; while (j < message.length()) { for (int i = 0; i < n; i++) { @@ -111,13 +111,13 @@ public class HillCipher { } // Determinant calculator - public static int determinant(int a[][], int n) { + public static int determinant(int[][] a, int n) { int det = 0, sign = 1, p = 0, q = 0; if (n == 1) { det = a[0][0]; } else { - int b[][] = new int[n - 1][n - 1]; + int[][] b = new int[n - 1][n - 1]; for (int x = 0; x < n; x++) { p = 0; q = 0; diff --git a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java index e2a33e035..c5ce8a9b1 100644 --- a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java @@ -4,7 +4,7 @@ import java.util.Scanner; class ProductCipher { - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the input to be encrypted: "); String substitutionInput = sc.nextLine(); diff --git a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java index b0ab817d0..fdf9df7b2 100644 --- a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java @@ -23,7 +23,7 @@ class BinaryToDecimal { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Binary number: "); System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong())); diff --git a/src/main/java/com/thealgorithms/conversions/BinaryToOctal.java b/src/main/java/com/thealgorithms/conversions/BinaryToOctal.java index b0d6b32fd..70bad8121 100644 --- a/src/main/java/com/thealgorithms/conversions/BinaryToOctal.java +++ b/src/main/java/com/thealgorithms/conversions/BinaryToOctal.java @@ -14,7 +14,7 @@ public class BinaryToOctal { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Input the binary number: "); int b = sc.nextInt(); diff --git a/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java index 7bd67123d..c87508c62 100644 --- a/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java @@ -12,7 +12,7 @@ class DecimalToBinary { * * @param args Command Line Arguments */ - public static void main(String args[]) { + public static void main(String[] args) { conventionalConversion(); bitwiseConversion(); } diff --git a/src/main/java/com/thealgorithms/conversions/HexToOct.java b/src/main/java/com/thealgorithms/conversions/HexToOct.java index ccbab30f0..2a57fbde5 100644 --- a/src/main/java/com/thealgorithms/conversions/HexToOct.java +++ b/src/main/java/com/thealgorithms/conversions/HexToOct.java @@ -52,7 +52,7 @@ public class HexToOct { * * @param args arguments */ - public static void main(String args[]) { + public static void main(String[] args) { String hexadecnum; int decnum, octalnum; Scanner scan = new Scanner(System.in); diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java index cb9d7fafd..7675c83eb 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java @@ -17,7 +17,7 @@ public class HexaDecimalToDecimal { } // Main method gets the hexadecimal input from user and converts it into Decimal output. - public static void main(String args[]) { + public static void main(String[] args) { String hexa_Input; int dec_output; Scanner scan = new Scanner(System.in); diff --git a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java index 782f34883..d4916a3db 100644 --- a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java @@ -14,7 +14,7 @@ public class OctalToDecimal { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Octal Input: "); String inputOctal = sc.nextLine(); diff --git a/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java b/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java index b1755b8a0..5edd94c38 100644 --- a/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java +++ b/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java @@ -46,7 +46,7 @@ public class OctalToHexadecimal { return hex; } - public static void main(String args[]) { + public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the Octal number: "); // Take octal number as input from user in a string diff --git a/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java b/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java index af26cc056..81c8d9bd1 100644 --- a/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java +++ b/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java @@ -14,7 +14,7 @@ public class TurkishToLatinConversion { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Input the string: "); String b = sc.next(); diff --git a/src/main/java/com/thealgorithms/datastructures/bags/Bag.java b/src/main/java/com/thealgorithms/datastructures/bags/Bag.java index 1d03e5cf4..c82b9124b 100644 --- a/src/main/java/com/thealgorithms/datastructures/bags/Bag.java +++ b/src/main/java/com/thealgorithms/datastructures/bags/Bag.java @@ -59,9 +59,8 @@ public class Bag implements Iterable { * @return true if bag contains element, otherwise false */ public boolean contains(Element element) { - Iterator iterator = this.iterator(); - while (iterator.hasNext()) { - if (iterator.next().equals(element)) { + for (Element value : this) { + if (value.equals(element)) { return true; } } diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java b/src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java index b640eeaf5..aba377329 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java @@ -6,7 +6,7 @@ class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Gr start vertex, end vertex and weights. Vertices should be labelled with a number between 0 and total number of vertices-1,both inclusive*/{ int vertex, edge; - private Edge edges[]; + private Edge[] edges; private int index = 0; BellmanFord(int v, int e) { @@ -36,7 +36,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number * @param p[] Parent array which shows updates in edges * @param i Current vertex under consideration */ - void printPath(int p[], int i) { + void printPath(int[] p, int i) { if (p[i] == -1) { // Found the path back to parent return; } @@ -44,7 +44,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number System.out.print(i + " "); } - public static void main(String args[]) { + public static void main(String[] args) { BellmanFord obj = new BellmanFord(0, 0); // Dummy object to call nonstatic variables obj.go(); } @@ -55,7 +55,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number System.out.println("Enter no. of vertices and edges please"); v = sc.nextInt(); e = sc.nextInt(); - Edge arr[] = new Edge[e]; // Array of edges + Edge[] arr = new Edge[e]; // Array of edges System.out.println("Input edges"); for (i = 0; i < e; i++) { u = sc.nextInt(); @@ -63,9 +63,9 @@ start vertex, end vertex and weights. Vertices should be labelled with a number w = sc.nextInt(); arr[i] = new Edge(u, ve, w); } - int dist[] = new int[v]; // Distance array for holding the finalized shortest path distance between source + int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance between source // and all vertices - int p[] = new int[v]; // Parent array for holding the paths + int[] p = new int[v]; // Parent array for holding the paths for (i = 0; i < v; i++) { dist[i] = Integer.MAX_VALUE; // Initializing distance values } @@ -113,11 +113,11 @@ start vertex, end vertex and weights. Vertices should be labelled with a number * @param end Ending vertex * @param Edge Array of edges */ - public void show(int source, int end, Edge arr[]) { // be created by using addEdge() method and passed by calling getEdgeArray() method // Just shows results of computation, if graph is passed to it. The graph should + public void show(int source, int end, Edge[] arr) { // be created by using addEdge() method and passed by calling getEdgeArray() method // Just shows results of computation, if graph is passed to it. The graph should int i, j, v = vertex, e = edge, neg = 0; - double dist[] = new double[v]; // Distance array for holding the finalized shortest path distance between source + double[] dist = new double[v]; // Distance array for holding the finalized shortest path distance between source // and all vertices - int p[] = new int[v]; // Parent array for holding the paths + int[] p = new int[v]; // Parent array for holding the paths for (i = 0; i < v; i++) { dist[i] = Integer.MAX_VALUE; // Initializing distance values } diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java b/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java index 5b8533b8d..31ed7ef2d 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java @@ -8,7 +8,7 @@ class dijkstras { int k = 9; - int minDist(int dist[], Boolean Set[]) { + int minDist(int[] dist, Boolean[] Set) { int min = Integer.MAX_VALUE, min_index = -1; for (int r = 0; r < k; r++) { @@ -21,16 +21,16 @@ class dijkstras { return min_index; } - void print(int dist[]) { + void print(int[] dist) { System.out.println("Vertex \t\t Distance"); for (int i = 0; i < k; i++) { System.out.println(i + " \t " + dist[i]); } } - void dijkstra(int graph[][], int src) { - int dist[] = new int[k]; - Boolean Set[] = new Boolean[k]; + void dijkstra(int[][] graph, int src) { + int[] dist = new int[k]; + Boolean[] Set = new Boolean[k]; for (int i = 0; i < k; i++) { dist[i] = Integer.MAX_VALUE; @@ -60,7 +60,7 @@ class dijkstras { } public static void main(String[] args) { - int graph[][] = new int[][] { + int[][] graph = new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java b/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java index ab9ef7352..bf3ef8e6e 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java @@ -4,7 +4,7 @@ import java.util.Scanner; public class FloydWarshall { - private int DistanceMatrix[][]; + private int[][] DistanceMatrix; private int numberofvertices; // number of vertices in the graph public static final int INFINITY = 999; @@ -15,7 +15,7 @@ public class FloydWarshall { this.numberofvertices = numberofvertices; } - public void floydwarshall(int AdjacencyMatrix[][]) { // calculates all the distances from source to destination vertex + public void floydwarshall(int[][] AdjacencyMatrix) { // calculates all the distances from source to destination vertex for (int source = 1; source <= numberofvertices; source++) { for ( int destination = 1; diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java b/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java index 8d19a8ca0..77cea399c 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java @@ -122,7 +122,7 @@ class AdjacencyListGraph> { public class Graphs { - public static void main(String args[]) { + public static void main(String[] args) { AdjacencyListGraph graph = new AdjacencyListGraph<>(); assert graph.addEdge(1, 2); assert graph.addEdge(1, 5); diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java index b3632b479..f24791dce 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java @@ -83,7 +83,7 @@ public class Kosaraju { } private void sortEdgesByLowestFinishTime(int v, List> list){ - int vis[] = new int[v]; + int[] vis = new int[v]; for (int i = 0; i < v; i++) { if(vis[i] == 0){ dfs(i, vis, list); @@ -110,7 +110,7 @@ public class Kosaraju { * @param transposeGraph Transpose of the given adjacency list */ public void findStronglyConnectedComponents(int v, List> transposeGraph){ - int vis[] = new int[v]; + int[] vis = new int[v]; while (!stack.isEmpty()) { var node = stack.pop(); if(vis[node] == 0){ @@ -122,7 +122,7 @@ public class Kosaraju { } //Dfs to store the nodes in order of lowest finish time - private void dfs(int node, int vis[], List> list){ + private void dfs(int node, int[] vis, List> list){ vis[node] = 1; for(Integer neighbour : list.get(node)){ if(vis[neighbour] == 0) @@ -132,7 +132,7 @@ public class Kosaraju { } //Dfs to find all the nodes of each strongly connected component - private void dfs2(int node, int vis[], List> list){ + private void dfs2(int node, int[] vis, List> list){ vis[node] = 1; for(Integer neighbour : list.get(node)){ if(vis[neighbour] == 0) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java b/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java index 8d382cdde..7593a9cfc 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java @@ -14,7 +14,7 @@ import java.util.Queue; */ public class MatrixGraphs { - public static void main(String args[]) { + public static void main(String[] args) { AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(10); graph.addEdge(1, 2); graph.addEdge(1, 5); diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java b/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java index 75de04713..893b835e0 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java @@ -12,7 +12,7 @@ class PrimMST { // A utility function to find the vertex with minimum key // value, from the set of vertices not yet included in MST - int minKey(int key[], Boolean mstSet[]) { + int minKey(int[] key, Boolean[] mstSet) { // Initialize min value int min = Integer.MAX_VALUE, min_index = -1; @@ -28,7 +28,7 @@ class PrimMST { // A utility function to print the constructed MST stored in // parent[] - void printMST(int parent[], int n, int graph[][]) { + void printMST(int[] parent, int n, int[][] graph) { System.out.println("Edge Weight"); for (int i = 1; i < V; i++) { System.out.println( @@ -39,15 +39,15 @@ class PrimMST { // Function to construct and print MST for a graph represented // using adjacency matrix representation - void primMST(int graph[][]) { + void primMST(int[][] graph) { // Array to store constructed MST - int parent[] = new int[V]; + int[] parent = new int[V]; // Key values used to pick minimum weight edge in cut - int key[] = new int[V]; + int[] key = new int[V]; // To represent set of vertices not yet included in MST - Boolean mstSet[] = new Boolean[V]; + Boolean[] mstSet = new Boolean[V]; // Initialize all keys as INFINITE for (int i = 0; i < V; i++) { @@ -103,7 +103,7 @@ class PrimMST { (3)-------(4) 9 */ PrimMST t = new PrimMST(); - int graph[][] = new int[][] { + int[][] graph = new int[][] { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 }, { 0, 3, 0, 0, 7 }, diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java index bb633c4ee..497daeca4 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java @@ -68,15 +68,15 @@ public class TarjansAlgorithm { // lowTime: indicates the earliest visited vertex (the vertex with minimum insertion time) that can // be reached from a subtree rooted with a particular node. - int lowTime[] = new int[V]; - int insertionTime[] = new int[V]; + int[] lowTime = new int[V]; + int[] insertionTime = new int[V]; for (int i = 0; i < V; i++) { insertionTime[i] = -1; lowTime[i] = -1; } // To check if element is present in stack - boolean isInStack[] = new boolean[V]; + boolean[] isInStack = new boolean[V]; // Store nodes during DFS Stack st = new Stack(); @@ -89,8 +89,8 @@ public class TarjansAlgorithm { return SCClist; } - private void stronglyConnCompsUtil(int u, int lowTime[], int insertionTime[], - boolean isInStack[], Stack st, List> graph) { + private void stronglyConnCompsUtil(int u, int[] lowTime, int[] insertionTime, + boolean[] isInStack, Stack st, List> graph) { // Initialize insertion time and lowTime value of current node insertionTime[u] = Time; @@ -101,22 +101,16 @@ public class TarjansAlgorithm { isInStack[u] = true; st.push(u); - int n; - // Go through all vertices adjacent to this - Iterator i = graph.get(u).iterator(); - - while (i.hasNext()) { - n = i.next(); - + for (Integer vertex : graph.get(u)) { //If the adjacent node is unvisited, do DFS - if (insertionTime[n] == -1) { - stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph); + if (insertionTime[vertex] == -1) { + stronglyConnCompsUtil(vertex, 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]) { + lowTime[u] = Math.min(lowTime[u], lowTime[vertex]); + } else if (isInStack[vertex]) { //If adj node is in stack, update low - lowTime[u] = Math.min(lowTime[u], insertionTime[n]); + lowTime[u] = Math.min(lowTime[u], insertionTime[vertex]); } } //If lowtime and insertion time are same, current node is the head of an SCC diff --git a/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java index 88f762ba5..2d048d996 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java @@ -133,7 +133,7 @@ class Link { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { DoublyLinkedList myList = new DoublyLinkedList(); LinkOperations linkOperations = new LinkOperations(); linkOperations.insertHead(13, myList); diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/MaximumMinimumWindow.java b/src/main/java/com/thealgorithms/datastructures/stacks/MaximumMinimumWindow.java index ecde496c0..53a502798 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/MaximumMinimumWindow.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/MaximumMinimumWindow.java @@ -39,8 +39,8 @@ public class MaximumMinimumWindow { */ public static int[] calculateMaxOfMin(int[] arr, int n) { Stack s = new Stack<>(); - int left[] = new int[n + 1]; - int right[] = new int[n + 1]; + int[] left = new int[n + 1]; + int[] right = new int[n + 1]; for (int i = 0; i < n; i++) { left[i] = -1; right[i] = n; @@ -74,7 +74,7 @@ public class MaximumMinimumWindow { s.push(i); } - int ans[] = new int[n + 1]; + int[] ans = new int[n + 1]; for (int i = 0; i <= n; i++) { ans[i] = 0; } @@ -96,7 +96,7 @@ public class MaximumMinimumWindow { return ans; } - public static void main(String args[]) { + public static void main(String[] args) { int[] arr = new int[] { 10, 20, 30, 50, 10, 70, 30 }; int[] target = new int[] { 70, 30, 20, 10, 10, 10, 10 }; int[] res = calculateMaxOfMin(arr, arr.length); diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/PostfixToInfix.java b/src/main/java/com/thealgorithms/datastructures/stacks/PostfixToInfix.java index 6b6ce7568..868aa778a 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/PostfixToInfix.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/PostfixToInfix.java @@ -118,7 +118,7 @@ public class PostfixToInfix { return infix; } - public static void main(String args[]) { + public static void main(String[] args) { assert getPostfixToInfix("ABC+/").equals("(A/(B+C))"); assert getPostfixToInfix("AB+CD+*").equals("((A+B)*(C+D))"); assert getPostfixToInfix("AB+C+D+").equals("(((A+B)+C)+D)"); diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java index 7fc761b4d..1dd9fe11d 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java @@ -10,7 +10,7 @@ import java.util.Stack; */ public class ReverseStack { - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println( "Enter the number of elements you wish to insert in the stack" diff --git a/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java b/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java index e44984b1b..5cd282022 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java @@ -3,7 +3,7 @@ package com.thealgorithms.datastructures.trees; public class FenwickTree { private int n; - private int fen_t[]; + private int[] fen_t; /* Constructor which takes the size of the array as a parameter */ public FenwickTree(int n) { diff --git a/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java b/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java index 68154129d..e24db38da 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java @@ -2,12 +2,12 @@ package com.thealgorithms.datastructures.trees; public class SegmentTree { - private int seg_t[]; + private int[] seg_t; private int n; - private int arr[]; + private int[] arr; /* Constructor which takes the size of the array and the array as a parameter*/ - public SegmentTree(int n, int arr[]) { + public SegmentTree(int n, int[] arr) { this.n = n; int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int seg_size = 2 * (int) Math.pow(2, x) - 1; diff --git a/src/main/java/com/thealgorithms/devutils/searches/MatrixSearchAlgorithm.java b/src/main/java/com/thealgorithms/devutils/searches/MatrixSearchAlgorithm.java index f102bd5c6..36587a21c 100644 --- a/src/main/java/com/thealgorithms/devutils/searches/MatrixSearchAlgorithm.java +++ b/src/main/java/com/thealgorithms/devutils/searches/MatrixSearchAlgorithm.java @@ -12,5 +12,5 @@ public interface MatrixSearchAlgorithm { * @param Comparable type * @return array containing the first found coordinates of the element */ - > int[] find(T matrix[][], T key); + > int[] find(T[][] matrix, T key); } diff --git a/src/main/java/com/thealgorithms/devutils/searches/SearchAlgorithm.java b/src/main/java/com/thealgorithms/devutils/searches/SearchAlgorithm.java index 69602811c..eb5b42756 100644 --- a/src/main/java/com/thealgorithms/devutils/searches/SearchAlgorithm.java +++ b/src/main/java/com/thealgorithms/devutils/searches/SearchAlgorithm.java @@ -12,5 +12,5 @@ public interface SearchAlgorithm { * @param Comparable type * @return first found index of the element */ - > int find(T array[], T key); + > int find(T[] array, T key); } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java b/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java index dfb75717b..0f2a14282 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java @@ -52,7 +52,7 @@ public class BoardPath { return count; } - public static int bpRS(int curr, int end, int strg[]) { + public static int bpRS(int curr, int end, int[] strg) { if (curr == end) { return 1; } else if (curr > end) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java index 49031152e..c6f555609 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java @@ -6,7 +6,7 @@ public class BruteForceKnapsack { // 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) { + static int knapSack(int W, int[] wt, int[] val, int n) { // Base Case if (n == 0 || W == 0) { return 0; @@ -29,9 +29,9 @@ public class BruteForceKnapsack { } // Driver code - public static void main(String args[]) { - int val[] = new int[] { 60, 100, 120 }; - int wt[] = new int[] { 10, 20, 30 }; + public static void main(String[] args) { + int[] val = new int[] { 60, 100, 120 }; + int[] wt = new int[] { 10, 20, 30 }; int W = 50; int n = val.length; System.out.println(knapSack(W, wt, val, n)); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java b/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java index 41bd49715..9744f3c03 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java @@ -23,7 +23,7 @@ public class CatalanNumber { */ static long findNthCatalan(int n) { // Array to store the results of subproblems i.e Catalan numbers from [1...n-1] - long catalanArray[] = new long[n + 1]; + long[] catalanArray = new long[n + 1]; // Initialising C₀ = 1 and C₁ = 1 catalanArray[0] = 1; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java index 968586c55..75189b61d 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java @@ -15,8 +15,8 @@ package com.thealgorithms.dynamicprogramming; public class CountFriendsPairing { - public static boolean countFriendsPairing(int n, int a[]) { - int dp[] = new int[n + 1]; + public static boolean countFriendsPairing(int n, int[] a) { + int[] dp = new int[n + 1]; // array of n+1 size is created dp[0] = 1; // since 1st index position value is fixed so it's marked as 1 diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/DyanamicProgrammingKnapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/DyanamicProgrammingKnapsack.java index 445f1e9d0..3b501f669 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/DyanamicProgrammingKnapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/DyanamicProgrammingKnapsack.java @@ -5,9 +5,9 @@ package com.thealgorithms.dynamicprogramming; public class DyanamicProgrammingKnapsack { // 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) { + static int knapSack(int W, int[] wt, int[] val, int n) { int i, w; - int K[][] = new int[n + 1][W + 1]; + int[][] K = new int[n + 1][W + 1]; // Build table K[][] in bottom up manner for (i = 0; i <= n; i++) { @@ -26,9 +26,9 @@ public class DyanamicProgrammingKnapsack { } // Driver code - public static void main(String args[]) { - int val[] = new int[] { 60, 100, 120 }; - int wt[] = new int[] { 10, 20, 30 }; + public static void main(String[] args) { + int[] val = new int[] { 60, 100, 120 }; + int[] wt = new int[] { 10, 20, 30 }; int W = 50; int n = val.length; System.out.println(knapSack(W, wt, val, n)); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java index efceb4494..eee6ab787 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java @@ -40,7 +40,7 @@ public class EggDropping { return eggFloor[n][m]; } - public static void main(String args[]) { + public static void main(String[] args) { int n = 2, m = 4; // result outputs min no. of trials in worst case for n eggs and m floors int result = minTrials(n, m); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java index 8123a0256..0c15e2feb 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java @@ -7,7 +7,7 @@ package com.thealgorithms.dynamicprogramming; public class KadaneAlgorithm { - public static boolean max_Sum(int a[], int predicted_answer) { + public static boolean max_Sum(int[] a, int predicted_answer) { int sum = a[0], running_sum = 0; for (int k : a) { running_sum = running_sum + k; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index df1bbd234..13296b845 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -5,13 +5,13 @@ package com.thealgorithms.dynamicprogramming; */ public class Knapsack { - private static int knapSack(int W, int wt[], int val[], int n) + private static int knapSack(int W, int[] wt, int[] val, int n) throws IllegalArgumentException { if (wt == null || val == null) { throw new IllegalArgumentException(); } int i, w; - int rv[][] = new int[n + 1][W + 1]; // rv means return value + int[][] rv = new int[n + 1][W + 1]; // rv means return value // Build table rv[][] in bottom up manner for (i = 0; i <= n; i++) { @@ -34,9 +34,9 @@ public class Knapsack { } // Driver program to test above function - public static void main(String args[]) { - int val[] = new int[] { 50, 100, 130 }; - int wt[] = new int[] { 10, 20, 40 }; + public static void main(String[] args) { + int[] val = new int[] { 50, 100, 130 }; + int[] wt = new int[] { 10, 20, 40 }; int W = 50; System.out.println(knapSack(W, wt, val, val.length)); } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java index 81888fda5..bcf1909d4 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java @@ -26,9 +26,8 @@ public class KnapsackMemoization { // Returns the value of maximum profit using recursive approach int solveKnapsackRecursive(int capacity, int[] weights, - int[] profits, int numOfItems, - int[][] dpTable) { - + int[] profits, int numOfItems, + int[][] dpTable) { // Base condition if (numOfItems == 0 || capacity == 0) { return 0; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java index e3786ac6b..bfa75a908 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java @@ -13,7 +13,7 @@ package com.thealgorithms.dynamicprogramming; public class LongestAlternatingSubsequence { /* Function to return longest alternating subsequence length*/ - static int AlternatingLength(int arr[], int n) { + static int AlternatingLength(int[] arr, int n) { /* las[i][0] = Length of the longest @@ -28,7 +28,7 @@ public class LongestAlternatingSubsequence { element */ - int las[][] = new int[n][2]; // las = LongestAlternatingSubsequence + int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence for (int i = 0; i < n; i++) { las[i][0] = las[i][1] = 1; @@ -61,7 +61,7 @@ public class LongestAlternatingSubsequence { } public static void main(String[] args) { - int arr[] = { 10, 22, 9, 33, 49, 50, 31, 60 }; + int[] arr = { 10, 22, 9, 33, 49, 50, 31, 60 }; int n = arr.length; System.out.println( "Length of Longest " + diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java index 3911d60bb..373077e88 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java @@ -11,7 +11,7 @@ public class LongestIncreasingSubsequence { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); - int arr[] = new int[n]; + int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } @@ -70,9 +70,9 @@ public class LongestIncreasingSubsequence { * @author Alon Firestein (https://github.com/alonfirestein) */ // A function for finding the length of the LIS algorithm in O(nlogn) complexity. - public static int findLISLen(int a[]) { + public static int findLISLen(int[] a) { int size = a.length; - int arr[] = new int[size]; + int[] arr = new int[size]; arr[0] = a[0]; int lis = 1; for (int i = 1; i < size; i++) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java index 824bce085..070427296 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java @@ -20,7 +20,7 @@ public class LongestPalindromicSubstring { if (input == null || input.length() == 0) { return input; } - boolean arr[][] = new boolean[input.length()][input.length()]; + boolean[][] arr = new boolean[input.length()][input.length()]; int start = 0, end = 0; for (int g = 0; g < input.length(); g++) { for (int i = 0, j = g; j < input.length(); i++, j++) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java b/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java index bf751e8c3..0bcff7678 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java @@ -8,9 +8,9 @@ package com.thealgorithms.dynamicprogramming; // minimizes the number of scalar multiplications. public class MatrixChainRecursiveTopDownMemoisation { - static int Memoized_Matrix_Chain(int p[]) { + static int Memoized_Matrix_Chain(int[] p) { int n = p.length; - int m[][] = new int[n][n]; + int[][] m = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = Integer.MAX_VALUE; @@ -19,7 +19,7 @@ public class MatrixChainRecursiveTopDownMemoisation { return Lookup_Chain(m, p, 1, n - 1); } - static int Lookup_Chain(int m[][], int p[], int i, int j) { + static int Lookup_Chain(int[][] m, int[] p, int i, int j) { if (i == j) { m[i][j] = 0; return m[i][j]; @@ -43,7 +43,7 @@ public class MatrixChainRecursiveTopDownMemoisation { // in this code we are taking the example of 4 matrixes whose orders are 1x2,2x3,3x4,4x5 respectively // output should be Minimum number of multiplications is 38 public static void main(String[] args) { - int arr[] = { 1, 2, 3, 4, 5 }; + int[] arr = { 1, 2, 3, 4, 5 }; System.out.println( "Minimum number of multiplications is " + Memoized_Matrix_Chain(arr) ); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index e52d72fd4..d41135b1f 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -10,7 +10,7 @@ package com.thealgorithms.dynamicprogramming; public class NewManShanksPrime { public static boolean nthManShanksPrime(int n, int expected_answer) { - int a[] = new int[n + 1]; + int[] a = new int[n + 1]; // array of n+1 size is initialized a[0] = a[1] = 1; // The 0th and 1st index position values are fixed. They are initialized as 1 diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java b/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java index 5994ffe8d..f30267ecc 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java @@ -134,7 +134,7 @@ public class RegexMatching { // Method 4: Bottom-Up DP(Tabulation) // Time Complexity=0(N*M) Space Complexity=0(N*M) static boolean regexBU(String src, String pat) { - boolean strg[][] = new boolean[src.length() + 1][pat.length() + 1]; + boolean[][] strg = new boolean[src.length() + 1][pat.length() + 1]; strg[src.length()][pat.length()] = true; for (int row = src.length(); row >= 0; row--) { for (int col = pat.length() - 1; col >= 0; col--) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 90369b6ff..066113a23 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -8,7 +8,7 @@ package com.thealgorithms.dynamicprogramming; public class RodCutting { private static int cutRod(int[] price, int n) { - int val[] = new int[n + 1]; + int[] val = new int[n + 1]; val[0] = 0; for (int i = 1; i <= n; i++) { @@ -24,7 +24,7 @@ public class RodCutting { } // main function to test - public static void main(String args[]) { + public static void main(String[] args) { int[] arr = new int[] { 2, 5, 13, 19, 20 }; int result = cutRod(arr, arr.length); System.out.println("Maximum Obtainable Value is " + result); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java b/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java index 722e0a898..c24bdeda6 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java @@ -45,7 +45,7 @@ class ShortestSuperSequence { } // Driver code - public static void main(String args[]) { + public static void main(String[] args) { String X = "AGGTAB"; String Y = "GXTXAYB"; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java index 2d36f5adf..46ba3999c 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java @@ -49,11 +49,11 @@ public class SubsetCount { */ public int getCountSO(int[] arr, int target){ int n = arr.length; - int prev[]=new int[target+1]; + int[] prev =new int[target+1]; prev[0] =1; if(arr[0]<=target) prev[arr[0]] = 1; for(int ind = 1; ind hm = new HashMap(); for (int i = 0; i < nums.length; i++) { hm.put(i, nums[i]); @@ -90,7 +90,7 @@ public class TwoSumProblem { public int[] HashMap(int[] nums, int target) { //Using Hashmaps - int ans[] = new int[2]; + int[] ans = new int[2]; HashMap hm = new HashMap(); for (int i = 0; i < nums.length; i++) { hm.put(nums[i], i); diff --git a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java index 1c7870e05..7e6ce9db4 100644 --- a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java @@ -26,11 +26,11 @@ public class BankersAlgorithm { * This method finds the need of each process */ static void calculateNeed( - int needArray[][], - int maxArray[][], - int allocationArray[][], - int totalProcess, - int totalResources + int[][] needArray, + int[][] maxArray, + int[][] allocationArray, + int totalProcess, + int totalResources ) { for (int i = 0; i < totalProcess; i++) { for (int j = 0; j < totalResources; j++) { @@ -55,12 +55,12 @@ public class BankersAlgorithm { * @return boolean if the system is in safe state or not */ static boolean checkSafeSystem( - int processes[], - int availableArray[], - int maxArray[][], - int allocationArray[][], - int totalProcess, - int totalResources + int[] processes, + int[] availableArray, + int[][] maxArray, + int[][] allocationArray, + int totalProcess, + int totalResources ) { int[][] needArray = new int[totalProcess][totalResources]; @@ -144,14 +144,14 @@ public class BankersAlgorithm { System.out.println("Enter total number of resources"); numberOfResources = sc.nextInt(); - int processes[] = new int[numberOfProcesses]; + int[] processes = new int[numberOfProcesses]; for (int i = 0; i < numberOfProcesses; i++) { processes[i] = i; } System.out.println("--Enter the availability of--"); - int availableArray[] = new int[numberOfResources]; + int[] availableArray = new int[numberOfResources]; for (int i = 0; i < numberOfResources; i++) { System.out.println("resource " + i + ": "); availableArray[i] = sc.nextInt(); @@ -159,7 +159,7 @@ public class BankersAlgorithm { System.out.println("--Enter the maximum matrix--"); - int maxArray[][] = new int[numberOfProcesses][numberOfResources]; + int[][] maxArray = new int[numberOfProcesses][numberOfResources]; for (int i = 0; i < numberOfProcesses; i++) { System.out.println("For process " + i + ": "); for (int j = 0; j < numberOfResources; j++) { @@ -172,7 +172,7 @@ public class BankersAlgorithm { System.out.println("--Enter the allocation matrix--"); - int allocationArray[][] = new int[numberOfProcesses][numberOfResources]; + int[][] allocationArray = new int[numberOfProcesses][numberOfResources]; for (int i = 0; i < numberOfProcesses; i++) { System.out.println("For process " + i + ": "); for (int j = 0; j < numberOfResources; j++) { diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index bb3a186b4..2e4edbd9a 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -35,10 +35,10 @@ public class BoyerMoore { return -1; } - public static void main(String args[]) { + public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); - int a[] = new int[n]; + int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } diff --git a/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java b/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java index 8730ed763..a1983fecc 100644 --- a/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java @@ -38,7 +38,7 @@ public class BrianKernighanAlgorithm { /** * @param args : command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int setBitCount = countSetBits(num); diff --git a/src/main/java/com/thealgorithms/others/CRCAlgorithm.java b/src/main/java/com/thealgorithms/others/CRCAlgorithm.java index 3b0d2f3a3..bfa8828e2 100644 --- a/src/main/java/com/thealgorithms/others/CRCAlgorithm.java +++ b/src/main/java/com/thealgorithms/others/CRCAlgorithm.java @@ -158,9 +158,7 @@ public class CRCAlgorithm { } dividedMessage = (ArrayList) x.clone(); if (!check) { - for (int z : dividedMessage) { - message.add(z); - } + message.addAll(dividedMessage); } else { if (dividedMessage.contains(1) && messageChanged) { wrongMessCaught++; diff --git a/src/main/java/com/thealgorithms/others/GuassLegendre.java b/src/main/java/com/thealgorithms/others/GuassLegendre.java index 9d1b169a0..8b15739a6 100644 --- a/src/main/java/com/thealgorithms/others/GuassLegendre.java +++ b/src/main/java/com/thealgorithms/others/GuassLegendre.java @@ -21,7 +21,7 @@ public class GuassLegendre { double a = 1, b = Math.pow(2, -0.5), t = 0.25, p = 1; for (int i = 0; i < l; ++i) { - double temp[] = update(a, b, t, p); + double[] temp = update(a, b, t, p); a = temp[0]; b = temp[1]; t = temp[2]; @@ -32,7 +32,7 @@ public class GuassLegendre { } static double[] update(double a, double b, double t, double p) { - double values[] = new double[4]; + double[] values = new double[4]; values[0] = (a + b) / 2; values[1] = Math.sqrt(a * b); values[2] = t - p * Math.pow(a - values[0], 2); diff --git a/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java b/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java index 08cdd44fb..3904691a9 100644 --- a/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java +++ b/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java @@ -10,7 +10,7 @@ class Trieac { // Trie node static class TrieNode { - TrieNode children[] = new TrieNode[ALPHABET_SIZE]; + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; // isWordEnd is true if the node represents // end of a word diff --git a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java index e38fab674..81697750e 100644 --- a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java +++ b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java @@ -8,7 +8,7 @@ public class InsertDeleteInArray { Scanner s = new Scanner(System.in); // Input statement System.out.println("Enter the size of the array"); int size = s.nextInt(); - int a[] = new int[size]; + int[] a = new int[size]; int i; // To enter the initial elements @@ -25,7 +25,7 @@ public class InsertDeleteInArray { System.out.println("Enter the element to be inserted"); int ins = s.nextInt(); int size2 = size + 1; - int b[] = new int[size2]; + int[] b = new int[size2]; for (i = 0; i < size2; i++) { if (i <= insert_pos) { b[i] = a[i]; diff --git a/src/main/java/com/thealgorithms/others/Krishnamurthy.java b/src/main/java/com/thealgorithms/others/Krishnamurthy.java index 2b0c61ff9..1f7cd1219 100644 --- a/src/main/java/com/thealgorithms/others/Krishnamurthy.java +++ b/src/main/java/com/thealgorithms/others/Krishnamurthy.java @@ -12,7 +12,7 @@ class Krishnamurthy { return p; } - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a, b, s = 0; System.out.print("Enter the number : "); diff --git a/src/main/java/com/thealgorithms/others/PageRank.java b/src/main/java/com/thealgorithms/others/PageRank.java index 8d67ff8f9..b5a7422ae 100644 --- a/src/main/java/com/thealgorithms/others/PageRank.java +++ b/src/main/java/com/thealgorithms/others/PageRank.java @@ -4,7 +4,7 @@ import java.util.*; class PageRank { - public static void main(String args[]) { + public static void main(String[] args) { int nodes, i, j; Scanner in = new Scanner(System.in); System.out.print("Enter the Number of WebPages: "); @@ -24,14 +24,14 @@ class PageRank { p.calc(nodes); } - public int path[][] = new int[10][10]; - public double pagerank[] = new double[10]; + public int[][] path = new int[10][10]; + public double[] pagerank = new double[10]; public void calc(double totalNodes) { double InitialPageRank; double OutgoingLinks = 0; double DampingFactor = 0.85; - double TempPageRank[] = new double[10]; + double[] TempPageRank = new double[10]; int ExternalNodeNumber; int InternalNodeNumber; int k = 1; // For Traversing diff --git a/src/main/java/com/thealgorithms/others/PasswordGen.java b/src/main/java/com/thealgorithms/others/PasswordGen.java index e1de72423..38a20841a 100644 --- a/src/main/java/com/thealgorithms/others/PasswordGen.java +++ b/src/main/java/com/thealgorithms/others/PasswordGen.java @@ -13,7 +13,7 @@ import java.util.Random; */ class PasswordGen { - public static void main(String args[]) { + public static void main(String[] args) { String password = generatePassword(8, 16); System.out.print("Password: " + password); } diff --git a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java index f73ce0f31..4bd3fa69c 100644 --- a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java +++ b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java @@ -119,7 +119,7 @@ public class QueueUsingTwoStacks { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { QueueWithStack myQueue = new QueueWithStack(); myQueue.insert(1); System.out.println(myQueue.peekBack()); // Will print 1 diff --git a/src/main/java/com/thealgorithms/others/RotateMatriceBy90Degree.java b/src/main/java/com/thealgorithms/others/RotateMatriceBy90Degree.java index 3c1b349e7..bc0461663 100644 --- a/src/main/java/com/thealgorithms/others/RotateMatriceBy90Degree.java +++ b/src/main/java/com/thealgorithms/others/RotateMatriceBy90Degree.java @@ -29,7 +29,7 @@ class Rotate_by_90_degree { sc.close(); } - static void printMatrix(int arr[][]) { + static void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { System.out.print(arr[i][j] + " "); @@ -44,7 +44,7 @@ class Rotate_by_90_degree { */ class Rotate { - static void rotate(int a[][]) { + static void rotate(int[][] a) { int n = a.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java index b951d9d91..08b0f9f40 100644 --- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java @@ -24,14 +24,11 @@ public class StackPostfixNotation { int num1 = s.pop(); String op = tokens.next(); - if (op.equals("+")) { - s.push(num1 + num2); - } else if (op.equals("-")) { - s.push(num1 - num2); - } else if (op.equals("*")) { - s.push(num1 * num2); - } else { - s.push(num1 / num2); + switch (op) { + case "+" -> s.push(num1 + num2); + case "-" -> s.push(num1 - num2); + case "*" -> s.push(num1 * num2); + default -> s.push(num1 / num2); } // "+", "-", "*", "/" } diff --git a/src/main/java/com/thealgorithms/others/Sudoku.java b/src/main/java/com/thealgorithms/others/Sudoku.java index 5ebe92c0c..574ca9837 100644 --- a/src/main/java/com/thealgorithms/others/Sudoku.java +++ b/src/main/java/com/thealgorithms/others/Sudoku.java @@ -100,7 +100,7 @@ class Sudoku { } // Driver Code - public static void main(String args[]) { + public static void main(String[] args) { int[][] board = new int[][] { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, diff --git a/src/main/java/com/thealgorithms/others/ThreeSum.java b/src/main/java/com/thealgorithms/others/ThreeSum.java index 7c9f3a1f2..299eaf4ee 100644 --- a/src/main/java/com/thealgorithms/others/ThreeSum.java +++ b/src/main/java/com/thealgorithms/others/ThreeSum.java @@ -18,11 +18,11 @@ import java.util.Scanner; */ class ThreeSum { - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // Length of an array - int a[] = new int[n]; + int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java index bc5b41580..1373748e0 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java @@ -42,10 +42,10 @@ class BinarySearch implements SearchAlgorithm { * @return the location of the key */ private > int search( - T array[], - T key, - int left, - int right + T[] array, + T key, + int left, + int right ) { if (right < left) { return -1; // this means that the key not found diff --git a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java index 0632971b7..f982598da 100644 --- a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java +++ b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java @@ -21,7 +21,7 @@ class InterpolationSearch { * @param key is a value what shoulb be found in the array * @return an index if the array contains the key unless -1 */ - public int find(int array[], int key) { + public int find(int[] array, int key) { // Find indexes of two corners int start = 0, end = (array.length - 1); diff --git a/src/main/java/com/thealgorithms/searches/KMPSearch.java b/src/main/java/com/thealgorithms/searches/KMPSearch.java index 223bc0669..c9b647fd3 100644 --- a/src/main/java/com/thealgorithms/searches/KMPSearch.java +++ b/src/main/java/com/thealgorithms/searches/KMPSearch.java @@ -8,7 +8,7 @@ class KMPSearch { // create lps[] that will hold the longest // prefix suffix values for pattern - int lps[] = new int[M]; + int[] lps = new int[M]; int j = 0; // index for pat[] // Preprocess the pattern (calculate lps[] @@ -38,7 +38,7 @@ class KMPSearch { return -1; } - void computeLPSArray(String pat, int M, int lps[]) { + void computeLPSArray(String pat, int M, int[] lps) { // length of the previous longest prefix suffix int len = 0; int i = 1; diff --git a/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java b/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java index 39f26a97d..1ff560049 100644 --- a/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java @@ -13,7 +13,7 @@ package com.thealgorithms.searches; public class OrderAgnosticBinarySearch { - static int BinSearchAlgo(int arr[], int start, int end, int target) { + 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]; diff --git a/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java b/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java index 5bcda7dd8..44d38a480 100644 --- a/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java +++ b/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java @@ -28,9 +28,9 @@ public class SaddlebackSearch { * @return The index(row and column) of the element if found. Else returns * -1 -1. */ - private static int[] find(int arr[][], int row, int col, int key) { + private static int[] find(int[][] arr, int row, int col, int key) { // array to store the answer row and column - int ans[] = { -1, -1 }; + int[] ans = { -1, -1 }; if (row < 0 || col >= arr[row].length) { return ans; } @@ -54,7 +54,7 @@ public class SaddlebackSearch { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); - int arr[][]; + int[][] arr; int i, j, rows = sc.nextInt(), col = sc.nextInt(); arr = new int[rows][col]; for (i = 0; i < rows; i++) { @@ -64,7 +64,7 @@ public class SaddlebackSearch { } int ele = sc.nextInt(); // we start from bottom left corner - int ans[] = find(arr, rows - 1, 0, ele); + int[] ans = find(arr, rows - 1, 0, ele); System.out.println(ans[0] + " " + ans[1]); sc.close(); } diff --git a/src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java b/src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java index 362fef91d..5b305831b 100644 --- a/src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java @@ -21,7 +21,7 @@ public class SquareRootBinarySearch { * * @param args Command line arguments */ - public static void main(String args[]) { + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print( "Enter a number you want to calculate square root of : " diff --git a/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java b/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java index 9f60b31b6..eb62555f5 100644 --- a/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java @@ -1,7 +1,7 @@ package com.thealgorithms.searches; import java.util.*; public class sortOrderAgnosticBinarySearch { - public static int find(int arr[],int key){ + public static int find(int[] arr, int key){ int start = 0; int end = arr.length-1; boolean arrDescending = arr[start]>arr[end]; //checking for Array is in ascending order or descending order. diff --git a/src/main/java/com/thealgorithms/sorts/BitonicSort.java b/src/main/java/com/thealgorithms/sorts/BitonicSort.java index 35acaf4de..414aa647e 100644 --- a/src/main/java/com/thealgorithms/sorts/BitonicSort.java +++ b/src/main/java/com/thealgorithms/sorts/BitonicSort.java @@ -8,7 +8,7 @@ public class BitonicSort { ASCENDING or DESCENDING; if (a[i] > a[j]) agrees with the direction, then a[i] and a[j] are interchanged. */ - void compAndSwap(int a[], int i, int j, int dir) { + void compAndSwap(int[] a, int i, int j, int dir) { if ((a[i] > a[j] && dir == 1) || (a[i] < a[j] && dir == 0)) { // Swapping elements int temp = a[i]; @@ -22,7 +22,7 @@ public class BitonicSort { (means dir=0). The sequence to be sorted starts at index position low, the parameter cnt is the number of elements to be sorted.*/ - void bitonicMerge(int a[], int low, int cnt, int dir) { + void bitonicMerge(int[] a, int low, int cnt, int dir) { if (cnt > 1) { int k = cnt / 2; for (int i = low; i < low + k; i++) { @@ -37,7 +37,7 @@ public class BitonicSort { recursively sorting its two halves in opposite sorting orders, and then calls bitonicMerge to make them in the same order */ - void bitonicSort(int a[], int low, int cnt, int dir) { + void bitonicSort(int[] a, int low, int cnt, int dir) { if (cnt > 1) { int k = cnt / 2; @@ -55,12 +55,12 @@ public class BitonicSort { /*Caller of bitonicSort for sorting the entire array of length N in ASCENDING order */ - void sort(int a[], int N, int up) { + void sort(int[] a, int N, int up) { bitonicSort(a, 0, N, up); } /* A utility function to print array of size n */ - static void printArray(int arr[]) { + static void printArray(int[] arr) { int n = arr.length; for (int i = 0; i < n; ++i) { System.out.print(arr[i] + " "); @@ -68,8 +68,8 @@ public class BitonicSort { System.out.println(); } - public static void main(String args[]) { - int a[] = { 3, 7, 4, 8, 6, 2, 1, 5 }; + public static void main(String[] args) { + int[] a = { 3, 7, 4, 8, 6, 2, 1, 5 }; int up = 1; BitonicSort ob = new BitonicSort(); ob.sort(a, a.length, up); diff --git a/src/main/java/com/thealgorithms/sorts/CycleSort.java b/src/main/java/com/thealgorithms/sorts/CycleSort.java index 6d21888e4..0852abfaa 100644 --- a/src/main/java/com/thealgorithms/sorts/CycleSort.java +++ b/src/main/java/com/thealgorithms/sorts/CycleSort.java @@ -74,7 +74,7 @@ class CycleSort implements SortAlgorithm { } public static void main(String[] args) { - Integer arr[] = { + Integer[] arr = { 4, 23, 6, diff --git a/src/main/java/com/thealgorithms/sorts/DNFSort.java b/src/main/java/com/thealgorithms/sorts/DNFSort.java index 912b67340..4c4520c63 100644 --- a/src/main/java/com/thealgorithms/sorts/DNFSort.java +++ b/src/main/java/com/thealgorithms/sorts/DNFSort.java @@ -4,7 +4,7 @@ public class DNFSort { // Sort the input array, the array is assumed to // have values in {0, 1, 2} - static void sort012(int a[], int arr_size) { + static void sort012(int[] a, int arr_size) { int low = 0; int high = arr_size - 1; int mid = 0, temp = 0; @@ -35,7 +35,7 @@ public class DNFSort { } /* Utility function to print array arr[] */ - static void printArray(int arr[], int arr_size) { + static void printArray(int[] arr, int arr_size) { for (int i = 0; i < arr_size; i++) { System.out.print(arr[i] + " "); } @@ -44,7 +44,7 @@ public class DNFSort { /*Driver function to check for above functions*/ public static void main(String[] args) { - int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; + int[] arr = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; int arr_size = arr.length; sort012(arr, arr_size); System.out.println("Array after seggregation "); diff --git a/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java b/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java index 9d6176c2d..69a09e675 100644 --- a/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java @@ -96,7 +96,7 @@ public class DualPivotQuickSort implements SortAlgorithm { * @param args the command line arguments */ public static void main(String[] args) { - Integer array[] = { 24, 8, -42, 75, -29, -77, 38, 57 }; + Integer[] array = { 24, 8, -42, 75, -29, -77, 38, 57 }; DualPivotQuickSort dualPivotQuickSort = new DualPivotQuickSort(); dualPivotQuickSort.sort(array); for (int i = 0; i < array.length; i++) { diff --git a/src/main/java/com/thealgorithms/sorts/LinkListSort.java b/src/main/java/com/thealgorithms/sorts/LinkListSort.java index 3c9ba1b7d..07f03cca0 100644 --- a/src/main/java/com/thealgorithms/sorts/LinkListSort.java +++ b/src/main/java/com/thealgorithms/sorts/LinkListSort.java @@ -10,12 +10,12 @@ import java.util.*; public class LinkListSort { - public static boolean isSorted(int p[], int option) { + public static boolean isSorted(int[] p, int option) { try (Scanner sc = new Scanner(System.in)) { } - int a[] = p; + int[] a = p; // Array is taken as input from test class - int b[] = p; + int[] b = p; // array similar to a int ch = option; // Choice is choosed as any number from 1 to 3 (So the linked list will be @@ -106,7 +106,7 @@ public class LinkListSort { return false; } - boolean compare(int a[], int b[]) { + boolean compare(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) return false; @@ -137,7 +137,7 @@ class Node { class Task { - static int a[]; + static int[] a; public Node sortByMergeSort(Node head) { if (head == null || head.next == null) @@ -171,7 +171,7 @@ class Task { // It will return a integer type value denoting the number of nodes present } - void task(int n[], int i, int j) { + void task(int[] n, int i, int j) { if (i < j) { int m = (i + j) / 2; task(n, i, m); @@ -181,9 +181,9 @@ class Task { } } - void task1(int n[], int s, int m, int e) { + void task1(int[] n, int s, int m, int e) { int i = s, k = 0, j = m + 1; - int b[] = new int[e - s + 1]; + int[] b = new int[e - s + 1]; while (i <= m && j <= e) { if (n[j] >= n[i]) b[k++] = n[i++]; @@ -210,7 +210,7 @@ class Task1 { if (head == null || head.next == null) return head; int c = count(head); - int a[] = new int[c]; + int[] a = new int[c]; // Array of size c is created a[0] = head.val; int i; @@ -247,7 +247,7 @@ class Task1 { class Task2 { - static int a[]; + static int[] a; public Node sortByHeapSort(Node head) { if (head == null || head.next == null) @@ -280,7 +280,7 @@ class Task2 { // It will return a integer type value denoting the number of nodes present } - void task(int n[]) { + void task(int[] n) { int k = n.length; for (int i = k / 2 - 1; i >= 0; i--) { task1(n, k, i); @@ -294,7 +294,7 @@ class Task2 { } } - void task1(int n[], int k, int i) { + void task1(int[] n, int k, int i) { int p = i; int l = 2 * i + 1; int r = 2 * i + 2; diff --git a/src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java b/src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java index 3953b5ed1..c06528608 100644 --- a/src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java +++ b/src/main/java/com/thealgorithms/sorts/MergeSortNoExtraSpace.java @@ -8,12 +8,12 @@ For understanding about mergesort visit :https://www.geeksforgeeks.org/merge-sor */ public class MergeSortNoExtraSpace { - public static void call_merge_sort(int a[], int n) { + public static void call_merge_sort(int[] a, int n) { int maxele = Arrays.stream(a).max().getAsInt() + 1; merge_sort(a, 0, n - 1, maxele); } - public static void merge_sort(int a[], int start, int end, int maxele) { //this function divides the array into 2 halves + public static void merge_sort(int[] a, int start, int end, int maxele) { //this function divides the array into 2 halves if (start < end) { int mid = (start + end) / 2; merge_sort(a, start, mid, maxele); @@ -23,11 +23,11 @@ public class MergeSortNoExtraSpace { } public static void implement_merge_sort( - int a[], - int start, - int mid, - int end, - int maxele + int[] a, + int start, + int mid, + int end, + int maxele ) { //implementation of mergesort int i = start; int j = mid + 1; @@ -58,11 +58,11 @@ public class MergeSortNoExtraSpace { } } - public static void main(String args[]) { + public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.println("Enter array size"); int n = inp.nextInt(); - int a[] = new int[n]; + int[] a = new int[n]; System.out.println("Enter array elements"); for (int i = 0; i < n; i++) { a[i] = inp.nextInt(); diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java index dcde8647f..5a9487da6 100644 --- a/src/main/java/com/thealgorithms/strings/Anagrams.java +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -50,8 +50,8 @@ public class Anagrams { if (s.length() != t.length()) { return false; } else { - char c[] = s.toCharArray(); - char d[] = t.toCharArray(); + char[] c = s.toCharArray(); + char[] d = t.toCharArray(); Arrays.sort(c); Arrays.sort( d @@ -65,8 +65,8 @@ public class Anagrams { if (a.length() != b.length()) { return false; } else { - int m[] = new int[26]; - int n[] = new int[26]; + int[] m = new int[26]; + int[] n = new int[26]; for (char c : a.toCharArray()) { m[c - 'a']++; } @@ -90,8 +90,8 @@ public class Anagrams { } // this is similar to approach number 2 but here the string is not converted to character array else { - int a[] = new int[26]; - int b[] = new int[26]; + int[] a = new int[26]; + int[] b = new int[26]; int k = s.length(); for (int i = 0; i < k; i++) { a[s.charAt(i) - 'a']++; diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 59ba954ef..fcdc310d4 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -44,7 +44,7 @@ public class LetterCombinationsOfPhoneNumber { // Driver code public static void main(String[] args) { - int number[] = { 2, 3, 4 }; + int[] number = { 2, 3, 4 }; printWords(number); } } diff --git a/src/main/java/com/thealgorithms/strings/MyAtoi.java b/src/main/java/com/thealgorithms/strings/MyAtoi.java index 0770f66c7..327cc4c19 100644 --- a/src/main/java/com/thealgorithms/strings/MyAtoi.java +++ b/src/main/java/com/thealgorithms/strings/MyAtoi.java @@ -22,18 +22,8 @@ public static int myAtoi(String s) { number = "0"; break; } - switch (ch) { - case '0' -> number += ch; - case '1' -> number += ch; - case '2' -> number += ch; - case '3' -> number += ch; - case '4' -> number += ch; - case '5' -> number += ch; - case '6' -> number += ch; - case '7' -> number += ch; - case '8' -> number += ch; - case '9' -> number += ch; - } + if(ch >= '0' && ch <= '9') + number += ch; } else if (ch == '-' && !isDigit) { number += "0"; negative = true; diff --git a/src/main/java/com/thealgorithms/strings/WordLadder.java b/src/main/java/com/thealgorithms/strings/WordLadder.java index a08d3d586..c4d8aeb91 100644 --- a/src/main/java/com/thealgorithms/strings/WordLadder.java +++ b/src/main/java/com/thealgorithms/strings/WordLadder.java @@ -48,10 +48,7 @@ class WordLadder { * if the endword is there. Otherwise, will return the length as 0. */ public static int ladderLength(String beginWord, String endWord, List wordList) { - HashSet set = new HashSet(); - for (String word : wordList) { - set.add(word); - } + HashSet set = new HashSet(wordList); if (!set.contains(endWord)) { return 0; diff --git a/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java b/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java index c2a1c4db7..139cd1070 100644 --- a/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java +++ b/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java @@ -9,7 +9,7 @@ public class AllPathsFromSourceToTargetTest { @Test void testForFirstCase() { int vertices = 4; - int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}}; + int[][] a = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}}; int source = 2; int destination = 3; List> list2 = List.of(List.of(2, 0, 1, 3),List.of(2, 0, 3),List.of(2, 1, 3)); @@ -21,7 +21,7 @@ public class AllPathsFromSourceToTargetTest { @Test void testForSecondCase() { int vertices = 5; - int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}}; + int[][] a = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}}; int source = 0; int destination = 4; List> list2 = List.of(List.of(0, 1, 3, 4),List.of(0, 1, 4),List.of(0, 2, 1, 3, 4),List.of(0, 2, 1, 4),List.of(0, 2, 4),List.of(0, 3, 4)); @@ -33,7 +33,7 @@ public class AllPathsFromSourceToTargetTest { @Test void testForThirdCase() { int vertices = 6; - int a[][] = {{1,0},{2,3},{0,4},{1,5},{4,3},{0,2},{0,3},{1,2},{0,5},{3,4},{2,5},{2,4}}; + int[][] a = {{1,0},{2,3},{0,4},{1,5},{4,3},{0,2},{0,3},{1,2},{0,5},{3,4},{2,5},{2,4}}; int source = 1; int destination = 5; List> list2 = List.of(List.of(1, 0, 2, 5),List.of(1, 0, 5),List.of(1, 5),List.of(1, 2, 5)); @@ -45,7 +45,7 @@ public class AllPathsFromSourceToTargetTest { @Test void testForFourthcase() { int vertices = 3; - int a[][] = {{0,1},{0,2},{1,2}}; + int[][] a = {{0,1},{0,2},{1,2}}; int source = 0; int destination = 2; List> list2 = List.of(List.of(0, 1, 2),List.of(0, 2)); diff --git a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java index 437ddb233..b46c7e0fe 100644 --- a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java +++ b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java @@ -8,8 +8,8 @@ class FloodFillTest { @Test void testForEmptyImage() { - int image[][] = {}; - int expected[][] = {}; + int[][] image = {}; + int[][] expected = {}; FloodFill.floodFill(image, 4, 5, 3, 2); assertArrayEquals(expected, image); @@ -17,8 +17,8 @@ class FloodFillTest { @Test void testForSingleElementImage() { - int image[][] = { { 1 } }; - int expected[][] = { { 3 } }; + int[][] image = { { 1 } }; + int[][] expected = { { 3 } }; FloodFill.floodFill(image, 0, 0, 3, 1); assertArrayEquals(expected, image); @@ -26,7 +26,7 @@ class FloodFillTest { @Test void testForImageOne() { - int image[][] = { + int[][] image = { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 3, 3, 3, 3, 0, 0 }, { 0, 3, 1, 1, 5, 0, 0 }, @@ -36,7 +36,7 @@ class FloodFillTest { { 0, 0, 0, 3, 3, 3, 3 }, }; - int expected[][] = { + int[][] expected = { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 3, 3, 3, 3, 0, 0 }, { 0, 3, 2, 2, 5, 0, 0 }, @@ -52,7 +52,7 @@ class FloodFillTest { @Test void testForImageTwo() { - int image[][] = { + int[][] image = { { 0, 0, 1, 1, 0, 0, 0 }, { 1, 1, 3, 3, 3, 0, 0 }, { 1, 3, 1, 1, 5, 0, 0 }, @@ -62,7 +62,7 @@ class FloodFillTest { { 0, 0, 0, 1, 3, 1, 3 }, }; - int expected[][] = { + int[][] expected = { { 0, 0, 2, 2, 0, 0, 0 }, { 2, 2, 3, 3, 3, 0, 0 }, { 2, 3, 2, 2, 5, 0, 0 }, @@ -78,13 +78,13 @@ class FloodFillTest { @Test void testForImageThree() { - int image[][] = { + int[][] image = { { 1, 1, 2, 3, 1, 1, 1 }, { 1, 0, 0, 1, 0, 0, 1 }, { 1, 1, 1, 0, 3, 1, 2 }, }; - int expected[][] = { + int[][] expected = { { 4, 4, 2, 3, 4, 4, 4 }, { 4, 0, 0, 4, 0, 0, 4 }, { 4, 4, 4, 0, 3, 4, 2 }, diff --git a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java index 97dc4d1b8..35f66f92e 100644 --- a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java +++ b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java @@ -45,7 +45,7 @@ public class MazeRecursionTest { MazeRecursion.setWay(map, 1, 1); MazeRecursion.setWay2(map2, 1, 1); - int expectedMap[][] = new int[][] { + int[][] expectedMap = new int[][] { { 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 0, 0, 0, 0, 1 }, { 1, 2, 2, 2, 0, 0, 1 }, @@ -56,7 +56,7 @@ public class MazeRecursionTest { { 1, 1, 1, 1, 1, 1, 1 }, }; - int expectedMap2[][] = new int[][] { + int[][] expectedMap2 = new int[][] { { 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 2, 2, 2, 2, 1 }, { 1, 0, 0, 0, 0, 2, 1 }, diff --git a/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java b/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java index 6bbd1c405..29125efe5 100644 --- a/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java +++ b/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java @@ -7,8 +7,8 @@ public class AutomorphicNumberTest { @Test void testAutomorphicNumber() { - int trueTestCases[] = { 0, 1, 25, 625, 12890625}; - int falseTestCases[] = { -5, 2, 26, 1234 }; + int[] trueTestCases = { 0, 1, 25, 625, 12890625}; + int[] falseTestCases = { -5, 2, 26, 1234 }; for (Integer n : trueTestCases) { assertTrue(AutomorphicNumber.isAutomorphic(n)); assertTrue(AutomorphicNumber.isAutomorphic2(n)); diff --git a/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java b/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java index 92512e99e..adaccff0a 100644 --- a/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java +++ b/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java @@ -7,8 +7,8 @@ class PerfectNumberTest { @Test public void perfectNumber() { - int trueTestCases[] = { 6, 28, 496, 8128, 33550336 }; - int falseTestCases[] = { -6, 0, 1, 9, 123 }; + int[] trueTestCases = { 6, 28, 496, 8128, 33550336 }; + int[] falseTestCases = { -6, 0, 1, 9, 123 }; for (Integer n : trueTestCases) { assertTrue(PerfectNumber.isPerfectNumber(n)); assertTrue(PerfectNumber.isPerfectNumber2(n)); diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java index 00cc1f534..89fc9b32e 100644 --- a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java +++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java @@ -9,49 +9,49 @@ public class CalculateMaxOfMinTest { @Test void testForOneElement() { - int a[] = { 10, 20, 30, 50, 10, 70, 30 }; + int[] a = { 10, 20, 30, 50, 10, 70, 30 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 70); } @Test void testForTwoElements() { - int a[] = { 5, 3, 2, 6, 3, 2, 6 }; + int[] a = { 5, 3, 2, 6, 3, 2, 6 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 6); } @Test void testForThreeElements() { - int a[] = { 10, 10, 10, 10, 10, 10, 10 }; + int[] a = { 10, 10, 10, 10, 10, 10, 10 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 10); } @Test void testForFourElements() { - int a[] = { 70, 60, 50, 40, 30, 20 }; + int[] a = { 70, 60, 50, 40, 30, 20 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 70); } @Test void testForFiveElements() { - int a[] = { 50 }; + int[] a = { 50 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 50); } @Test void testForSixElements() { - int a[] = { 1, 4, 7, 9, 2, 4, 6 }; + int[] a = { 1, 4, 7, 9, 2, 4, 6 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == 9); } @Test void testForSevenElements() { - int a[] = { -1, -5, -7, -9, -12, -14 }; + int[] a = { -1, -5, -7, -9, -12, -14 }; int k = CalculateMaxOfMin.calculateMaxOfMin(a); assertTrue(k == -1); } diff --git a/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java index 681573bb7..e30ffd423 100644 --- a/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java +++ b/src/test/java/com/thealgorithms/others/CountFriendsPairingTest.java @@ -9,49 +9,49 @@ public class CountFriendsPairingTest { @Test void testForOneElement() { - int a[] = { 1, 2, 2 }; + int[] a = { 1, 2, 2 }; assertTrue(CountFriendsPairing.countFriendsPairing(3, a)); } @Test void testForTwoElements() { - int a[] = { 1, 2, 2, 3 }; + int[] a = { 1, 2, 2, 3 }; assertTrue(CountFriendsPairing.countFriendsPairing(4, a)); } @Test void testForThreeElements() { - int a[] = { 1, 2, 2, 3, 3 }; + int[] a = { 1, 2, 2, 3, 3 }; assertTrue(CountFriendsPairing.countFriendsPairing(5, a)); } @Test void testForFourElements() { - int a[] = { 1, 2, 2, 3, 3, 4 }; + int[] a = { 1, 2, 2, 3, 3, 4 }; assertTrue(CountFriendsPairing.countFriendsPairing(6, a)); } @Test void testForFiveElements() { - int a[] = { 1, 2, 2, 3, 3, 4, 4 }; + int[] a = { 1, 2, 2, 3, 3, 4, 4 }; assertTrue(CountFriendsPairing.countFriendsPairing(7, a)); } @Test void testForSixElements() { - int a[] = { 1, 2, 2, 3, 3, 4, 4, 4 }; + int[] a = { 1, 2, 2, 3, 3, 4, 4, 4 }; assertTrue(CountFriendsPairing.countFriendsPairing(8, a)); } @Test void testForSevenElements() { - int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5 }; + int[] a = { 1, 2, 2, 3, 3, 4, 4, 4, 5 }; assertTrue(CountFriendsPairing.countFriendsPairing(9, a)); } @Test void testForEightElements() { - int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5 }; + int[] a = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5 }; assertTrue(CountFriendsPairing.countFriendsPairing(10, a)); } } diff --git a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java index d740f01cc..74ca9a3ef 100644 --- a/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java +++ b/src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java @@ -9,49 +9,49 @@ public class KadaneAlogrithmTest { @Test void testForOneElement() { - int a[] = { -1 }; + int[] a = { -1 }; assertTrue(KadaneAlgorithm.max_Sum(a, -1)); } @Test void testForTwoElements() { - int a[] = { -2, 1 }; + int[] a = { -2, 1 }; assertTrue(KadaneAlgorithm.max_Sum(a, 1)); } @Test void testForThreeElements() { - int a[] = { 5, 3, 12 }; + int[] a = { 5, 3, 12 }; assertTrue(KadaneAlgorithm.max_Sum(a, 20)); } @Test void testForFourElements() { - int a[] = { -1, -3, -7, -4 }; + int[] a = { -1, -3, -7, -4 }; assertTrue(KadaneAlgorithm.max_Sum(a, -1)); } @Test void testForFiveElements() { - int a[] = { 4, 5, 3, 0, 2 }; + int[] a = { 4, 5, 3, 0, 2 }; assertTrue(KadaneAlgorithm.max_Sum(a, 14)); } @Test void testForSixElements() { - int a[] = { -43, -45, 47, 12, 87, -13 }; + int[] a = { -43, -45, 47, 12, 87, -13 }; assertTrue(KadaneAlgorithm.max_Sum(a, 146)); } @Test void testForSevenElements() { - int a[] = { 9, 8, 2, 23, 13, 6, 7 }; + int[] a = { 9, 8, 2, 23, 13, 6, 7 }; assertTrue(KadaneAlgorithm.max_Sum(a, 68)); } @Test void testForEightElements() { - int a[] = { 9, -5, -5, -2, 4, 5, 0, 1 }; + int[] a = { 9, -5, -5, -2, 4, 5, 0, 1 }; assertTrue(KadaneAlgorithm.max_Sum(a, 10)); } } diff --git a/src/test/java/com/thealgorithms/others/LinkListSortTest.java b/src/test/java/com/thealgorithms/others/LinkListSortTest.java index f16c48dd1..7c6da59cb 100644 --- a/src/test/java/com/thealgorithms/others/LinkListSortTest.java +++ b/src/test/java/com/thealgorithms/others/LinkListSortTest.java @@ -9,49 +9,49 @@ public class LinkListSortTest { @Test void testForOneElement() { - int a[] = { 56 }; + int[] a = { 56 }; assertTrue(LinkListSort.isSorted(a, 2)); } @Test void testForTwoElements() { - int a[] = { 6, 4 }; + int[] a = { 6, 4 }; assertTrue(LinkListSort.isSorted(a, 1)); } @Test void testForThreeElements() { - int a[] = { 875, 253, 12 }; + int[] a = { 875, 253, 12 }; assertTrue(LinkListSort.isSorted(a, 3)); } @Test void testForFourElements() { - int a[] = { 86, 32, 87, 13 }; + int[] a = { 86, 32, 87, 13 }; assertTrue(LinkListSort.isSorted(a, 1)); } @Test void testForFiveElements() { - int a[] = { 6, 5, 3, 0, 9 }; + int[] a = { 6, 5, 3, 0, 9 }; assertTrue(LinkListSort.isSorted(a, 1)); } @Test void testForSixElements() { - int a[] = { 9, 65, 432, 32, 47, 327 }; + int[] a = { 9, 65, 432, 32, 47, 327 }; assertTrue(LinkListSort.isSorted(a, 3)); } @Test void testForSevenElements() { - int a[] = { 6, 4, 2, 1, 3, 6, 7 }; + int[] a = { 6, 4, 2, 1, 3, 6, 7 }; assertTrue(LinkListSort.isSorted(a, 1)); } @Test void testForEightElements() { - int a[] = { 123, 234, 145, 764, 322, 367, 768, 34 }; + int[] a = { 123, 234, 145, 764, 322, 367, 768, 34 }; assertTrue(LinkListSort.isSorted(a, 2)); } } diff --git a/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java index 37f1aa403..804cfc3e9 100644 --- a/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java @@ -8,7 +8,7 @@ public class sortOrderAgnosticBinarySearchTest{ @Test public void testAscending(){ - int arr[] = {1,2,3,4,5};// for ascending order. + int[] arr = {1,2,3,4,5};// for ascending order. int target = 2; int ans=sortOrderAgnosticBinarySearch.find(arr, target); int excepted = 1; @@ -17,7 +17,7 @@ public class sortOrderAgnosticBinarySearchTest{ @Test public void testDescending(){ - int arr[] = {5,4,3,2,1};// for descending order. + int[] arr = {5,4,3,2,1};// for descending order. int target = 2; int ans=sortOrderAgnosticBinarySearch.find(arr, target); int excepted = 3;