General performance improvement (#6078)

This commit is contained in:
Strange Developer
2024-11-01 18:52:42 +01:00
committed by GitHub
parent 7b962a4a1d
commit df0c997e4b
29 changed files with 92 additions and 75 deletions

View File

@ -2418,8 +2418,6 @@ public final class AES {
rBytes = new StringBuilder(rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2));
}
// t = new BigInteger(rBytes, 16);
// return t;
return new BigInteger(rBytes.toString(), 16);
}

View File

@ -34,19 +34,19 @@ final class AffineCipher {
*/
static String encryptMessage(char[] msg) {
// Cipher Text initially empty
String cipher = "";
StringBuilder cipher = new StringBuilder();
for (int i = 0; i < msg.length; i++) {
// Avoid space to be encrypted
/* applying encryption formula ( a * x + b ) mod m
{here x is msg[i] and m is 26} and added 'A' to
bring it in the range of ASCII alphabet [65-90 | A-Z] */
if (msg[i] != ' ') {
cipher += (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
cipher.append((char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'));
} else { // else simply append space character
cipher += msg[i];
cipher.append(msg[i]);
}
}
return cipher;
return cipher.toString();
}
/**
@ -56,7 +56,7 @@ final class AffineCipher {
* @return the decrypted plaintext message
*/
static String decryptCipher(String cipher) {
String msg = "";
StringBuilder msg = new StringBuilder();
int aInv = 0;
int flag;
@ -75,13 +75,13 @@ final class AffineCipher {
{here x is cipher[i] and m is 26} and added 'A'
to bring it in the range of ASCII alphabet [65-90 | A-Z] */
if (cipher.charAt(i) != ' ') {
msg += (char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A');
msg.append((char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A'));
} else { // else simply append space character
msg += cipher.charAt(i);
msg.append(cipher.charAt(i));
}
}
return msg;
return msg.toString();
}
// Driver code

View File

@ -1078,7 +1078,7 @@ public class Blowfish {
* @return String object which is a binary representation of the hex number passed as parameter
*/
private String hexToBin(String hex) {
String binary = "";
StringBuilder binary = new StringBuilder();
long num;
String binary4B;
int n = hex.length();
@ -1089,9 +1089,9 @@ public class Blowfish {
binary4B = "0000" + binary4B;
binary4B = binary4B.substring(binary4B.length() - 4);
binary += binary4B;
binary.append(binary4B);
}
return binary;
return binary.toString();
}
/**
@ -1103,12 +1103,12 @@ public class Blowfish {
*/
private String binToHex(String binary) {
long num = Long.parseUnsignedLong(binary, 2);
String hex = Long.toHexString(num);
StringBuilder hex = new StringBuilder(Long.toHexString(num));
while (hex.length() < (binary.length() / 4)) {
hex = "0" + hex;
hex.insert(0, "0");
}
return hex;
return hex.toString();
}
/**
@ -1121,12 +1121,12 @@ public class Blowfish {
private String xor(String a, String b) {
a = hexToBin(a);
b = hexToBin(b);
String ans = "";
StringBuilder ans = new StringBuilder();
for (int i = 0; i < a.length(); i++) {
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
ans.append((char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0'));
}
ans = binToHex(ans);
return ans;
ans = new StringBuilder(binToHex(ans.toString()));
return ans.toString();
}
/**

View File

@ -136,7 +136,7 @@ public final class AnyBaseToAnyBase {
int decimalValue = 0;
int charB2;
char charB1;
String output = "";
StringBuilder output = new StringBuilder();
// Go through every character of n
for (int i = 0; i < n.length(); i++) {
// store the character in charB1
@ -167,15 +167,15 @@ public final class AnyBaseToAnyBase {
// If the remainder is a digit < 10, simply add it to
// the left side of the new number.
if (decimalValue % b2 < 10) {
output = decimalValue % b2 + output;
output.insert(0, decimalValue % b2);
} // If the remainder is >= 10, add a character with the
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
else {
output = (char) ((decimalValue % b2) + 55) + output;
output.insert(0, (char) ((decimalValue % b2) + 55));
}
// Divide by the new base again
decimalValue /= b2;
}
return output;
return output.toString();
}
}

View File

@ -52,9 +52,11 @@ public class HexaDecimalToBinary {
*/
public String completeDigits(String binNum) {
final int byteSize = 8;
while (binNum.length() < byteSize) {
binNum = "0" + binNum;
StringBuilder binNumBuilder = new StringBuilder(binNum);
while (binNumBuilder.length() < byteSize) {
binNumBuilder.insert(0, "0");
}
binNum = binNumBuilder.toString();
return binNum;
}
}

View File

@ -42,9 +42,7 @@ public class FloydWarshall {
public void floydwarshall(int[][] adjacencyMatrix) {
// Initialize the distance matrix with the adjacency matrix.
for (int source = 1; source <= numberofvertices; source++) {
for (int destination = 1; destination <= numberofvertices; destination++) {
distanceMatrix[source][destination] = adjacencyMatrix[source][destination];
}
System.arraycopy(adjacencyMatrix[source], 1, distanceMatrix[source], 1, numberofvertices);
}
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) {
for (int source = 1; source <= numberofvertices; source++) {

View File

@ -403,7 +403,7 @@ public class SinglyLinkedList implements Iterable<Integer> {
SinglyLinkedList list = new SinglyLinkedList();
assert list.isEmpty();
assert list.size() == 0 && list.count() == 0;
assert list.toString().equals("");
assert list.toString().isEmpty();
/* Test insert function */
list.insertHead(5);

View File

@ -23,6 +23,7 @@ public class GenericTree {
}
private final Node root;
public GenericTree() { // Constructor
Scanner scn = new Scanner(System.in);
root = createTreeG(null, 0, scn);
@ -225,8 +226,6 @@ public class GenericTree {
for (int i = 0; i < node.child.size(); i++) {
if (node.child.get(i).child.size() == 0) {
arr.add(i);
// node.child.remove(i);
// i--;
} else {
removeleaves(node.child.get(i));
}

View File

@ -1,3 +1,5 @@
package com.thealgorithms.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

View File

@ -1,7 +1,8 @@
package com.thealgorithms.maths;
import static java.util.Collections.singletonList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.lang3.tuple.MutablePair;
@ -16,7 +17,7 @@ import org.apache.commons.lang3.tuple.MutablePair;
* - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers
*/
public class NthUglyNumber {
private ArrayList<Long> uglyNumbers = new ArrayList<>(Arrays.asList(1L));
private ArrayList<Long> uglyNumbers = new ArrayList<>(singletonList(1L));
private ArrayList<MutablePair<Integer, Integer>> positions = new ArrayList<>();
/**

View File

@ -33,8 +33,7 @@ public final class VampireNumber {
// System.out.println(i+ " "+ j);
if (isVampireNumber(i, j, true)) {
countofRes++;
res.append("" + countofRes + ": = ( " + i + "," + j + " = " + i * j + ")"
+ "\n");
res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n");
}
}
}

View File

@ -1,5 +1,6 @@
package com.thealgorithms.scheduling;
import java.util.Collections;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
@ -72,9 +73,7 @@ public final class NonPreemptivePriorityScheduling {
int index = 0;
Process[] executionOrder = new Process[processes.length];
for (Process process : processes) {
waitingQueue.add(process);
}
Collections.addAll(waitingQueue, processes);
while (!waitingQueue.isEmpty() || !pq.isEmpty()) {
// Add processes that have arrived to the priority queue