diff --git a/checkstyle.xml b/checkstyle.xml index 8b659d98..de7c7026 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -111,7 +111,7 @@ - + diff --git a/src/main/java/com/thealgorithms/ciphers/Blowfish.java b/src/main/java/com/thealgorithms/ciphers/Blowfish.java index d0e94720..a8fa6fc5 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[][] sBox = { { "d1310ba6", "98dfb5ac", @@ -1047,7 +1047,7 @@ public class Blowfish { }; // Initializing subkeys with digits of pi - String[] P = { + String[] subKeys = { "243f6a88", "85a308d3", "13198a2e", @@ -1154,7 +1154,7 @@ public class Blowfish { for (int i = 0; i < 8; i += 2) { // column number for S-box is a 8-bit value long col = Long.parseUnsignedLong(hexToBin(plainText.substring(i, i + 2)), 2); - a[i / 2] = S[i / 2][(int) col]; + a[i / 2] = sBox[i / 2][(int) col]; } ans = addBin(a[0], a[1]); ans = xor(ans, a[2]); @@ -1165,9 +1165,9 @@ public class Blowfish { // generate subkeys private void keyGenerate(String key) { int j = 0; - for (int i = 0; i < P.length; i++) { + for (int i = 0; i < subKeys.length; i++) { // XOR-ing 32-bit parts of the key with initial subkeys - P[i] = xor(P[i], key.substring(j, j + 8)); + subKeys[i] = xor(subKeys[i], key.substring(j, j + 8)); j = (j + 8) % key.length(); } @@ -1179,7 +1179,7 @@ public class Blowfish { String right; left = plainText.substring(0, 8); right = plainText.substring(8, 16); - left = xor(left, P[time]); + left = xor(left, subKeys[time]); // output from F function String fOut = f(left); @@ -1207,8 +1207,8 @@ public class Blowfish { // postprocessing String right = plainText.substring(0, 8); String left = plainText.substring(8, 16); - right = xor(right, P[16]); - left = xor(left, P[17]); + right = xor(right, subKeys[16]); + left = xor(left, subKeys[17]); return left + right; } @@ -1229,8 +1229,8 @@ public class Blowfish { // postprocessing String right = cipherText.substring(0, 8); String left = cipherText.substring(8, 16); - right = xor(right, P[1]); - left = xor(left, P[0]); + right = xor(right, subKeys[1]); + left = xor(left, subKeys[0]); return left + right; } } diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index 5372c95f..c766b83f 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -3,7 +3,7 @@ package com.thealgorithms.conversions; // Hex [0-9],[A-F] -> Binary [0,1] public class HexaDecimalToBinary { - private final int LONG_BITS = 8; + private final int longBits = 8; public String convert(String numHex) { // String a HexaDecimal: @@ -15,7 +15,7 @@ public class HexaDecimalToBinary { } public String completeDigits(String binNum) { - for (int i = binNum.length(); i < LONG_BITS; i++) { + for (int i = binNum.length(); i < longBits; i++) { binNum = "0" + binNum; } return binNum; diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java b/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java index ced55d87..25b01bce 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java @@ -17,7 +17,7 @@ import java.util.Map; */ class GCounter { - private final Map P; + private final Map counterMap; private final int myId; private final int n; @@ -29,10 +29,10 @@ class GCounter { GCounter(int myId, int n) { this.myId = myId; this.n = n; - this.P = new HashMap<>(); + this.counterMap = new HashMap<>(); for (int i = 0; i < n; i++) { - P.put(i, 0); + counterMap.put(i, 0); } } @@ -40,7 +40,7 @@ class GCounter { * Increments the counter for the current node. */ public void increment() { - P.put(myId, P.get(myId) + 1); + counterMap.put(myId, counterMap.get(myId) + 1); } /** @@ -50,7 +50,7 @@ class GCounter { */ public int value() { int sum = 0; - for (int v : P.values()) { + for (int v : counterMap.values()) { sum += v; } return sum; @@ -64,7 +64,7 @@ class GCounter { */ public boolean compare(GCounter other) { for (int i = 0; i < n; i++) { - if (this.P.get(i) > other.P.get(i)) { + if (this.counterMap.get(i) > other.counterMap.get(i)) { return false; } } @@ -78,7 +78,7 @@ class GCounter { */ public void merge(GCounter other) { for (int i = 0; i < n; i++) { - this.P.put(i, Math.max(this.P.get(i), other.P.get(i))); + this.counterMap.put(i, Math.max(this.counterMap.get(i), other.counterMap.get(i))); } } } diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java b/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java index 04b84675..53c21dcb 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java @@ -17,8 +17,8 @@ import java.util.Map; */ class PNCounter { - private final Map P; - private final Map N; + private final Map pCounter; + private final Map nCounter; private final int myId; private final int n; @@ -31,12 +31,12 @@ class PNCounter { PNCounter(int myId, int n) { this.myId = myId; this.n = n; - this.P = new HashMap<>(); - this.N = new HashMap<>(); + this.pCounter = new HashMap<>(); + this.nCounter = new HashMap<>(); for (int i = 0; i < n; i++) { - P.put(i, 0); - N.put(i, 0); + pCounter.put(i, 0); + nCounter.put(i, 0); } } @@ -44,14 +44,14 @@ class PNCounter { * Increments the increment counter for the current node. */ public void increment() { - P.put(myId, P.get(myId) + 1); + pCounter.put(myId, pCounter.get(myId) + 1); } /** * Increments the decrement counter for the current node. */ public void decrement() { - N.put(myId, N.get(myId) + 1); + nCounter.put(myId, nCounter.get(myId) + 1); } /** @@ -60,8 +60,8 @@ class PNCounter { * @return The total value of the counter. */ public int value() { - int sumP = P.values().stream().mapToInt(Integer::intValue).sum(); - int sumN = N.values().stream().mapToInt(Integer::intValue).sum(); + int sumP = pCounter.values().stream().mapToInt(Integer::intValue).sum(); + int sumN = nCounter.values().stream().mapToInt(Integer::intValue).sum(); return sumP - sumN; } @@ -76,7 +76,7 @@ class PNCounter { throw new IllegalArgumentException("Cannot compare PN-Counters with different number of nodes"); } for (int i = 0; i < n; i++) { - if (this.P.get(i) > other.P.get(i) && this.N.get(i) > other.N.get(i)) { + if (this.pCounter.get(i) > other.pCounter.get(i) && this.nCounter.get(i) > other.nCounter.get(i)) { return false; } } @@ -93,8 +93,8 @@ class PNCounter { throw new IllegalArgumentException("Cannot merge PN-Counters with different number of nodes"); } for (int i = 0; i < n; i++) { - this.P.put(i, Math.max(this.P.get(i), other.P.get(i))); - this.N.put(i, Math.max(this.N.get(i), other.N.get(i))); + this.pCounter.put(i, Math.max(this.pCounter.get(i), other.pCounter.get(i))); + this.nCounter.put(i, Math.max(this.nCounter.get(i), other.nCounter.get(i))); } } } diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java b/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java index b295fb08..a574b40d 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java @@ -4,12 +4,12 @@ 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; public FloydWarshall(int numberofvertices) { - DistanceMatrix = new int[numberofvertices + 1][numberofvertices + 1]; // stores the value of distance from all the possible path form the source + distanceMatrix = new int[numberofvertices + 1][numberofvertices + 1]; // stores the value of distance from all the possible path form the source // vertex to destination vertex // The matrix is initialized with 0's by default this.numberofvertices = numberofvertices; @@ -18,17 +18,17 @@ public class FloydWarshall { 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; destination <= numberofvertices; destination++) { - DistanceMatrix[source][destination] = AdjacencyMatrix[source][destination]; + distanceMatrix[source][destination] = AdjacencyMatrix[source][destination]; } } for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) { for (int source = 1; source <= numberofvertices; source++) { for (int destination = 1; destination <= numberofvertices; destination++) { - if (DistanceMatrix[source][intermediate] + DistanceMatrix[intermediate][destination] < DistanceMatrix[source][destination]) { // calculated distance it get replaced as + if (distanceMatrix[source][intermediate] + distanceMatrix[intermediate][destination] < distanceMatrix[source][destination]) { // calculated distance it get replaced as // new shortest distance // if the new // distance calculated is less then the // earlier shortest - DistanceMatrix[source][destination] = DistanceMatrix[source][intermediate] + DistanceMatrix[intermediate][destination]; + distanceMatrix[source][destination] = distanceMatrix[source][intermediate] + distanceMatrix[intermediate][destination]; } } } @@ -40,7 +40,7 @@ public class FloydWarshall { for (int source = 1; source <= numberofvertices; source++) { System.out.print(source + "\t"); for (int destination = 1; destination <= numberofvertices; destination++) { - System.out.print(DistanceMatrix[source][destination] + "\t"); + System.out.print(distanceMatrix[source][destination] + "\t"); } System.out.println(); } diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java index d12f631d..65483eee 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -8,7 +8,7 @@ package com.thealgorithms.datastructures.graphs; */ public class HamiltonianCycle { - private int V; + private int vertex; private int pathCount; private int[] cycle; private int[][] graph; @@ -22,8 +22,8 @@ public class HamiltonianCycle { * else returns 1D array with value -1. */ public int[] findHamiltonianCycle(int[][] graph) { - this.V = graph.length; - this.cycle = new int[this.V + 1]; + this.vertex = graph.length; + this.cycle = new int[this.vertex + 1]; // Initialize path array with -1 value for (int i = 0; i < this.cycle.length; i++) { @@ -53,17 +53,17 @@ public class HamiltonianCycle { * @returns true if path is found false otherwise */ public boolean isPathFound(int vertex) { - boolean isLastVertexConnectedToStart = this.graph[vertex][0] == 1 && this.pathCount == this.V; + boolean isLastVertexConnectedToStart = this.graph[vertex][0] == 1 && this.pathCount == this.vertex; if (isLastVertexConnectedToStart) { return true; } /** all vertices selected but last vertex not linked to 0 **/ - if (this.pathCount == this.V) { + if (this.pathCount == this.vertex) { return false; } - for (int v = 0; v < this.V; v++) { + for (int v = 0; v < this.vertex; v++) { /** if connected **/ if (this.graph[vertex][v] == 1) { /** add to path **/ diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java b/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java index c5780534..902553f9 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java @@ -48,17 +48,17 @@ class AdjacencyMatrixGraph { /** * The number of vertices in the graph */ - private int _numberOfVertices; + private int vertexCount; /** * The number of edges in the graph */ - private int _numberOfEdges; + private int edgeCount; /** * The adjacency matrix for the graph */ - private int[][] _adjacency; + private int[][] adjMatrix; /** * Static variables to define whether or not an edge exists in the adjacency @@ -87,16 +87,16 @@ class AdjacencyMatrixGraph { * @param newNumberOfVertices the new number of vertices */ private void setNumberOfVertices(int newNumberOfVertices) { - this._numberOfVertices = newNumberOfVertices; + this.vertexCount = newNumberOfVertices; } /** - * Getter for `this._numberOfVertices` + * Getter for `this.vertexCount` * * @return the number of vertices in the graph */ public int numberOfVertices() { - return this._numberOfVertices; + return this.vertexCount; } /** @@ -106,16 +106,16 @@ class AdjacencyMatrixGraph { * */ private void setNumberOfEdges(int newNumberOfEdges) { - this._numberOfEdges = newNumberOfEdges; + this.edgeCount = newNumberOfEdges; } /** - * Getter for `this._numberOfEdges` + * Getter for `this.edgeCount` * * @return the number of edges */ public int numberOfEdges() { - return this._numberOfEdges; + return this.edgeCount; } /** @@ -124,7 +124,7 @@ class AdjacencyMatrixGraph { * @param newAdjacency the new adjaceny matrix */ private void setAdjacency(int[][] newAdjacency) { - this._adjacency = newAdjacency; + this.adjMatrix = newAdjacency; } /** @@ -133,7 +133,7 @@ class AdjacencyMatrixGraph { * @return the adjacency matrix */ private int[][] adjacency() { - return this._adjacency; + return this.adjMatrix; } /** @@ -222,12 +222,12 @@ class AdjacencyMatrixGraph { */ public List depthFirstOrder(int startVertex) { // If the startVertex is invalid, return an empty list - if (startVertex >= _numberOfVertices || startVertex < 0) { + if (startVertex >= vertexCount || startVertex < 0) { return new ArrayList(); } // Create an array to track the visited vertices - boolean[] visited = new boolean[_numberOfVertices]; + boolean[] visited = new boolean[vertexCount]; // Create a list to keep track of the order of our traversal ArrayList orderList = new ArrayList(); @@ -259,7 +259,7 @@ class AdjacencyMatrixGraph { orderList.add(currentVertex); // Get the adjacency array for this vertex - int[] adjacent = _adjacency[currentVertex]; + int[] adjacent = adjMatrix[currentVertex]; for (int i = 0; i < adjacent.length; i++) { // we are considering exploring, recurse on it // If an edge exists between the // currentVertex and the vertex if (adjacent[i] == AdjacencyMatrixGraph.EDGE_EXIST) { @@ -277,12 +277,12 @@ class AdjacencyMatrixGraph { */ public List breadthFirstOrder(int startVertex) { // If the specified startVertex is invalid, return an empty list - if (startVertex >= _numberOfVertices || startVertex < 0) { + if (startVertex >= vertexCount || startVertex < 0) { return new ArrayList(); } // Create an array to keep track of the visited vertices - boolean[] visited = new boolean[_numberOfVertices]; + boolean[] visited = new boolean[vertexCount]; // Create a list to keep track of the ordered vertices ArrayList orderList = new ArrayList(); @@ -309,7 +309,7 @@ class AdjacencyMatrixGraph { // Get the adjacency array for the currentVertex and // check each node - int[] adjacent = _adjacency[currentVertex]; + int[] adjacent = adjMatrix[currentVertex]; for (int vertex = 0; vertex < adjacent.length; vertex++) { // vertex we are considering exploring, we add it to the queue // If an // edge exists between the current vertex and the if (adjacent[vertex] == AdjacencyMatrixGraph.EDGE_EXIST) { @@ -336,7 +336,7 @@ class AdjacencyMatrixGraph { for (int i = 0; i < this.numberOfVertices(); i++) { s = s + i + " : "; for (int j = 0; j < this.numberOfVertices(); j++) { - s = s + this._adjacency[i][j] + " "; + s = s + this.adjMatrix[i][j] + " "; } s = s + "\n"; } diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java index 293ea583..251c169d 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java @@ -56,9 +56,9 @@ import java.util.Stack; public class TarjansAlgorithm { // Timer for tracking lowtime and insertion time - private int Time; + private int time; - private List> SCClist = new ArrayList>(); + private List> sccList = new ArrayList>(); public List> stronglyConnectedComponents(int V, List> graph) { @@ -85,15 +85,15 @@ public class TarjansAlgorithm { if (insertionTime[i] == -1) stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph); } - return SCClist; + return sccList; } 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; - lowTime[u] = Time; - Time += 1; + insertionTime[u] = time; + lowTime[u] = time; + time += 1; // Push current node into stack isInStack[u] = true; @@ -123,7 +123,7 @@ public class TarjansAlgorithm { scc.add(w); isInStack[w] = false; } - SCClist.add(scc); + sccList.add(scc); } } } diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java index 6f382766..b48502b5 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java @@ -12,21 +12,21 @@ public class HashMapCuckooHashing { private int tableSize; // size of the hash table private Integer[] buckets; // array representing the table - private final Integer AVAILABLE; + private final Integer emptySlot; private int size; // number of elements in the hash table private int thresh; // threshold for infinite loop checking /** * Constructor initializes buckets array, hsize, and creates dummy object - * for AVAILABLE + * for emptySlot * * @param tableSize the desired size of the hash map */ public HashMapCuckooHashing(int tableSize) { this.buckets = new Integer[tableSize]; this.tableSize = tableSize; - this.AVAILABLE = Integer.MIN_VALUE; + this.emptySlot = Integer.MIN_VALUE; this.size = 0; this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2; } @@ -84,7 +84,7 @@ public class HashMapCuckooHashing { loopCounter++; hash = hashFunction1(key); - if ((buckets[hash] == null) || Objects.equals(buckets[hash], AVAILABLE)) { + if ((buckets[hash] == null) || Objects.equals(buckets[hash], emptySlot)) { buckets[hash] = wrappedInt; size++; checkLoadFactor(); @@ -95,7 +95,7 @@ public class HashMapCuckooHashing { buckets[hash] = wrappedInt; wrappedInt = temp; hash = hashFunction2(temp); - if (Objects.equals(buckets[hash], AVAILABLE)) { + if (Objects.equals(buckets[hash], emptySlot)) { buckets[hash] = wrappedInt; size++; checkLoadFactor(); @@ -124,7 +124,7 @@ public class HashMapCuckooHashing { public void reHashTableIncreasesTableSize() { HashMapCuckooHashing newT = new HashMapCuckooHashing(tableSize * 2); for (int i = 0; i < tableSize; i++) { - if (buckets[i] != null && !Objects.equals(buckets[i], AVAILABLE)) { + if (buckets[i] != null && !Objects.equals(buckets[i], emptySlot)) { newT.insertKey2HashTable(this.buckets[i]); } } @@ -146,14 +146,14 @@ public class HashMapCuckooHashing { } if (Objects.equals(buckets[hash], wrappedInt)) { - buckets[hash] = AVAILABLE; + buckets[hash] = emptySlot; size--; return; } hash = hashFunction2(key); if (Objects.equals(buckets[hash], wrappedInt)) { - buckets[hash] = AVAILABLE; + buckets[hash] = emptySlot; size--; return; } @@ -165,7 +165,7 @@ public class HashMapCuckooHashing { */ public void displayHashtable() { for (int i = 0; i < tableSize; i++) { - if ((buckets[i] == null) || Objects.equals(buckets[i], AVAILABLE)) { + if ((buckets[i] == null) || Objects.equals(buckets[i], emptySlot)) { System.out.println("Bucket " + i + ": Empty"); } else { System.out.println("Bucket " + i + ": " + buckets[i].toString()); @@ -229,7 +229,7 @@ public class HashMapCuckooHashing { public boolean isFull() { boolean response = true; for (int i = 0; i < tableSize; i++) { - if (buckets[i] == null || Objects.equals(buckets[i], AVAILABLE)) { + if (buckets[i] == null || Objects.equals(buckets[i], emptySlot)) { return false; } } diff --git a/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java index b9331569..ec1e15e4 100644 --- a/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java +++ b/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java @@ -15,7 +15,7 @@ public class GenericArrayListQueue { /** * The generic ArrayList for the queue T is the generic element */ - ArrayList _queue = new ArrayList<>(); + ArrayList elementList = new ArrayList<>(); /** * Checks if the queue has elements (not empty). @@ -23,7 +23,7 @@ public class GenericArrayListQueue { * @return True if the queue has elements. False otherwise. */ private boolean hasElements() { - return !_queue.isEmpty(); + return !elementList.isEmpty(); } /** @@ -35,7 +35,7 @@ public class GenericArrayListQueue { public T peek() { T result = null; if (this.hasElements()) { - result = _queue.get(0); + result = elementList.get(0); } return result; } @@ -47,7 +47,7 @@ public class GenericArrayListQueue { * @return True if the element was added successfully */ public boolean add(T element) { - return _queue.add(element); + return elementList.add(element); } /** @@ -58,7 +58,7 @@ public class GenericArrayListQueue { public T pull() { T result = null; if (this.hasElements()) { - result = _queue.remove(0); + result = elementList.remove(0); } return result; } diff --git a/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java b/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java index 5cd28202..5378a01f 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java @@ -3,12 +3,12 @@ package com.thealgorithms.datastructures.trees; public class FenwickTree { private int n; - private int[] fen_t; + private int[] fenTree; /* Constructor which takes the size of the array as a parameter */ public FenwickTree(int n) { this.n = n; - this.fen_t = new int[n + 1]; + this.fenTree = new int[n + 1]; } /* A function which will add the element val at index i*/ @@ -16,7 +16,7 @@ public class FenwickTree { // As index starts from 0, increment the index by 1 i += 1; while (i <= n) { - fen_t[i] += val; + fenTree[i] += val; i += i & (-i); } } @@ -27,7 +27,7 @@ public class FenwickTree { i += 1; int cumSum = 0; while (i > 0) { - cumSum += fen_t[i]; + cumSum += fenTree[i]; i -= i & (-i); } return cumSum; diff --git a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java index c7cb108d..2961282e 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java @@ -7,13 +7,13 @@ import java.util.Scanner; */ public class RedBlackBST { - private final int R = 0; - private final int B = 1; + private final int red = 0; + private final int black = 1; private class Node { int key = -1; - int color = B; + int color = black; Node left = nil; Node right = nil; Node p = nil; @@ -31,7 +31,7 @@ public class RedBlackBST { return; } printTree(node.left); - System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); + System.out.print(((node.color == red) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); printTree(node.right); } @@ -39,7 +39,7 @@ public class RedBlackBST { if (node == nil) { return; } - System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); + System.out.print(((node.color == red) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); printTreepre(node.left); printTreepre(node.right); } @@ -66,10 +66,10 @@ public class RedBlackBST { Node temp = root; if (root == nil) { root = node; - node.color = B; + node.color = black; node.p = nil; } else { - node.color = R; + node.color = red; while (true) { if (node.key < temp.key) { if (temp.left == nil) { @@ -94,15 +94,15 @@ public class RedBlackBST { } private void fixTree(Node node) { - while (node.p.color == R) { + while (node.p.color == red) { Node y = nil; if (node.p == node.p.p.left) { y = node.p.p.right; - if (y != nil && y.color == R) { - node.p.color = B; - y.color = B; - node.p.p.color = R; + if (y != nil && y.color == red) { + node.p.color = black; + y.color = black; + node.p.p.color = red; node = node.p.p; continue; } @@ -110,15 +110,15 @@ public class RedBlackBST { node = node.p; rotateLeft(node); } - node.p.color = B; - node.p.p.color = R; + node.p.color = black; + node.p.p.color = red; rotateRight(node.p.p); } else { y = node.p.p.left; - if (y != nil && y.color == R) { - node.p.color = B; - y.color = B; - node.p.p.color = R; + if (y != nil && y.color == red) { + node.p.color = black; + y.color = black; + node.p.p.color = red; node = node.p.p; continue; } @@ -126,12 +126,12 @@ public class RedBlackBST { node = node.p; rotateRight(node); } - node.p.color = B; - node.p.p.color = R; + node.p.color = black; + node.p.p.color = red; rotateLeft(node.p.p); } } - root.color = B; + root.color = black; } void rotateLeft(Node node) { @@ -234,67 +234,67 @@ public class RedBlackBST { y.left.p = y; y.color = z.color; } - if (yorigcolor == B) { + if (yorigcolor == black) { deleteFixup(x); } return true; } void deleteFixup(Node x) { - while (x != root && x.color == B) { + while (x != root && x.color == black) { if (x == x.p.left) { Node w = x.p.right; - if (w.color == R) { - w.color = B; - x.p.color = R; + if (w.color == red) { + w.color = black; + x.p.color = red; rotateLeft(x.p); w = x.p.right; } - if (w.left.color == B && w.right.color == B) { - w.color = R; + if (w.left.color == black && w.right.color == black) { + w.color = red; x = x.p; continue; - } else if (w.right.color == B) { - w.left.color = B; - w.color = R; + } else if (w.right.color == black) { + w.left.color = black; + w.color = red; rotateRight(w); w = x.p.right; } - if (w.right.color == R) { + if (w.right.color == red) { w.color = x.p.color; - x.p.color = B; - w.right.color = B; + x.p.color = black; + w.right.color = black; rotateLeft(x.p); x = root; } } else { Node w = x.p.left; - if (w.color == R) { - w.color = B; - x.p.color = R; + if (w.color == red) { + w.color = black; + x.p.color = red; rotateRight(x.p); w = x.p.left; } - if (w.right.color == B && w.left.color == B) { - w.color = R; + if (w.right.color == black && w.left.color == black) { + w.color = red; x = x.p; continue; - } else if (w.left.color == B) { - w.right.color = B; - w.color = R; + } else if (w.left.color == black) { + w.right.color = black; + w.color = red; rotateLeft(w); w = x.p.left; } - if (w.left.color == R) { + if (w.left.color == red) { w.color = x.p.color; - x.p.color = B; - w.left.color = B; + x.p.color = black; + w.left.color = black; rotateRight(x.p); x = root; } } } - x.color = B; + x.color = black; } public void insertDemo() { diff --git a/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java b/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java index a6954b24..a6a76f8f 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java @@ -2,7 +2,7 @@ package com.thealgorithms.datastructures.trees; public class SegmentTree { - private int[] seg_t; + private int[] segTree; private int n; private int[] arr; @@ -12,7 +12,7 @@ public class SegmentTree { int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int segSize = 2 * (int) Math.pow(2, x) - 1; - this.seg_t = new int[segSize]; + this.segTree = new int[segSize]; this.arr = arr; this.n = n; constructTree(arr, 0, n - 1, 0); @@ -21,13 +21,13 @@ public class SegmentTree { /* A function which will create the segment tree*/ public final int constructTree(int[] arr, int start, int end, int index) { if (start == end) { - this.seg_t[index] = arr[start]; + this.segTree[index] = arr[start]; return arr[start]; } int mid = start + (end - start) / 2; - this.seg_t[index] = constructTree(arr, start, mid, index * 2 + 1) + constructTree(arr, mid + 1, end, index * 2 + 2); - return this.seg_t[index]; + this.segTree[index] = constructTree(arr, start, mid, index * 2 + 1) + constructTree(arr, mid + 1, end, index * 2 + 2); + return this.segTree[index]; } /* A function which will update the value at a index i. This will be called by the @@ -37,7 +37,7 @@ public class SegmentTree { return; } - this.seg_t[seg_index] += diff; + this.segTree[seg_index] += diff; if (start != end) { int mid = start + (end - start) / 2; updateTree(start, mid, index, diff, seg_index * 2 + 1); @@ -60,7 +60,7 @@ public class SegmentTree { * internally*/ private int getSumTree(int start, int end, int q_start, int q_end, int seg_index) { if (q_start <= start && q_end >= end) { - return this.seg_t[seg_index]; + return this.segTree[seg_index]; } if (q_start > end || q_end < start) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/OptimalJobScheduling.java b/src/main/java/com/thealgorithms/dynamicprogramming/OptimalJobScheduling.java index 5ee33275..0840e08c 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/OptimalJobScheduling.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/OptimalJobScheduling.java @@ -13,24 +13,24 @@ public class OptimalJobScheduling { private final int numberProcesses; private final int numberMachines; - private final int[][] Run; - private final int[][] Transfer; - private final int[][] Cost; + private final int[][] run; + private final int[][] transfer; + private final int[][] cost; /** * Constructor of the class. * @param numberProcesses ,refers to the number of precedent processes(N) * @param numberMachines ,refers to the number of different machines in our disposal(M) - * @param Run , N*M matrix refers to the cost of running each process to each machine - * @param Transfer ,M*M symmetric matrix refers to the transportation delay for each pair of + * @param run , N*M matrix refers to the cost of running each process to each machine + * @param transfer ,M*M symmetric matrix refers to the transportation delay for each pair of * machines */ - public OptimalJobScheduling(int numberProcesses, int numberMachines, int[][] Run, int[][] Transfer) { + public OptimalJobScheduling(int numberProcesses, int numberMachines, int[][] run, int[][] transfer) { this.numberProcesses = numberProcesses; this.numberMachines = numberMachines; - this.Run = Run; - this.Transfer = Transfer; - this.Cost = new int[numberProcesses][numberMachines]; + this.run = run; + this.transfer = transfer; + this.cost = new int[numberProcesses][numberMachines]; } /** @@ -50,7 +50,7 @@ public class OptimalJobScheduling { for (int j = 0; j < numberMachines; j++) { // for each Machine - Cost[i][j] = runningCost(i, j); + cost[i][j] = runningCost(i, j); } } } @@ -71,7 +71,7 @@ public class OptimalJobScheduling { if (process == 0) // refers to the first process,which does not require for a previous one // to have been executed - return Run[process][machine]; + return run[process][machine]; else { int[] runningCosts = new int[numberMachines]; // stores the costs of executing our Process depending on @@ -79,7 +79,7 @@ public class OptimalJobScheduling { for (int k = 0; k < numberMachines; k++) // computes the cost of executing the previous // process to each and every Machine - runningCosts[k] = Cost[process - 1][k] + Transfer[k][machine] + Run[process][machine]; // transferring the result to our Machine and executing + runningCosts[k] = cost[process - 1][k] + transfer[k][machine] + run[process][machine]; // transferring the result to our Machine and executing // the Process to our Machine return findMin(runningCosts); // returns the minimum running cost @@ -88,19 +88,19 @@ public class OptimalJobScheduling { /** * Function used in order to return the minimum Cost. - * @param cost ,an Array of size M which refers to the costs of executing a Process to each + * @param costArr ,an Array of size M which refers to the costs of executing a Process to each * Machine * @return the minimum cost */ - private int findMin(int[] cost) { + private int findMin(int[] costArr) { int min = 0; - for (int i = 1; i < cost.length; i++) { + for (int i = 1; i < costArr.length; i++) { - if (cost[i] < cost[min]) min = i; + if (costArr[i] < costArr[min]) min = i; } - return cost[min]; + return costArr[min]; } /** @@ -111,7 +111,7 @@ public class OptimalJobScheduling { for (int i = 0; i < numberProcesses; i++) { for (int j = 0; j < numberMachines; j++) { - System.out.print(Cost[i][j]); + System.out.print(cost[i][j]); System.out.print(" "); } @@ -124,6 +124,6 @@ public class OptimalJobScheduling { * Getter for the running Cost of i process on j machine. */ public int getCost(int process, int machine) { - return Cost[process][machine]; + return cost[process][machine]; } } diff --git a/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java b/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java index 17a1d237..1ec45a86 100644 --- a/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java +++ b/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; class StrassenMatrixMultiplicationTest { - StrassenMatrixMultiplication SMM = new StrassenMatrixMultiplication(); + StrassenMatrixMultiplication smm = new StrassenMatrixMultiplication(); // Strassen Matrix Multiplication can only be allplied to matrices of size 2^n // and has to be a Square Matrix @@ -16,7 +16,7 @@ class StrassenMatrixMultiplicationTest { int[][] a = {{1, 2}, {3, 4}}; int[][] b = {{5, 6}, {7, 8}}; int[][] expResult = {{19, 22}, {43, 50}}; - int[][] actResult = SMM.multiply(a, b); + int[][] actResult = smm.multiply(a, b); assertArrayEquals(expResult, actResult); } @@ -25,7 +25,7 @@ class StrassenMatrixMultiplicationTest { int[][] a = {{1, 2, 5, 4}, {9, 3, 0, 6}, {4, 6, 3, 1}, {0, 2, 0, 6}}; int[][] b = {{1, 0, 4, 1}, {1, 2, 0, 2}, {0, 3, 1, 3}, {1, 8, 1, 2}}; int[][] expResult = {{7, 51, 13, 28}, {18, 54, 42, 27}, {11, 29, 20, 27}, {8, 52, 6, 16}}; - int[][] actResult = SMM.multiply(a, b); + int[][] actResult = smm.multiply(a, b); assertArrayEquals(expResult, actResult); } @@ -35,7 +35,7 @@ class StrassenMatrixMultiplicationTest { int[][] a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; int[][] b = {{1, -2, -3, 4}, {4, -3, -2, 1}, {5, -6, -7, 8}, {8, -7, -6, -5}}; int[][] expResult = {{56, -54, -52, 10}, {128, -126, -124, 42}, {200, -198, -196, 74}, {272, -270, -268, 106}}; - int[][] actResult = SMM.multiply(a, b); + int[][] actResult = smm.multiply(a, b); assertArrayEquals(expResult, actResult); } } diff --git a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java index 2c355cee..bdd07029 100644 --- a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java @@ -6,14 +6,14 @@ import org.junit.jupiter.api.Test; class BinaryInsertionSortTest { - BinaryInsertionSort BIS = new BinaryInsertionSort(); + BinaryInsertionSort bis = new BinaryInsertionSort(); @Test // valid test case public void binaryInsertionSortTestNonDuplicate() { int[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7}; int[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int[] actResult = BIS.binaryInsertSort(array); + int[] actResult = bis.binaryInsertSort(array); assertArrayEquals(expResult, actResult); } @@ -21,7 +21,7 @@ class BinaryInsertionSortTest { public void binaryInsertionSortTestDuplicate() { int[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6}; int[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9}; - int[] actResult = BIS.binaryInsertSort(array); + int[] actResult = bis.binaryInsertSort(array); assertArrayEquals(expResult, actResult); } }