Skip to content

Commit df0c997

Browse files
authored
General performance improvement (TheAlgorithms#6078)
1 parent 7b962a4 commit df0c997

29 files changed

+92
-75
lines changed

src/main/java/com/thealgorithms/ciphers/AES.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,8 +2418,6 @@ public static BigInteger scheduleCore(BigInteger t, int rconCounter) {
24182418
rBytes = new StringBuilder(rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2));
24192419
}
24202420

2421-
// t = new BigInteger(rBytes, 16);
2422-
// return t;
24232421
return new BigInteger(rBytes.toString(), 16);
24242422
}
24252423

src/main/java/com/thealgorithms/ciphers/AffineCipher.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ private AffineCipher() {
3434
*/
3535
static String encryptMessage(char[] msg) {
3636
// Cipher Text initially empty
37-
String cipher = "";
37+
StringBuilder cipher = new StringBuilder();
3838
for (int i = 0; i < msg.length; i++) {
3939
// Avoid space to be encrypted
4040
/* applying encryption formula ( a * x + b ) mod m
4141
{here x is msg[i] and m is 26} and added 'A' to
4242
bring it in the range of ASCII alphabet [65-90 | A-Z] */
4343
if (msg[i] != ' ') {
44-
cipher += (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
44+
cipher.append((char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'));
4545
} else { // else simply append space character
46-
cipher += msg[i];
46+
cipher.append(msg[i]);
4747
}
4848
}
49-
return cipher;
49+
return cipher.toString();
5050
}
5151

5252
/**
@@ -56,7 +56,7 @@ static String encryptMessage(char[] msg) {
5656
* @return the decrypted plaintext message
5757
*/
5858
static String decryptCipher(String cipher) {
59-
String msg = "";
59+
StringBuilder msg = new StringBuilder();
6060
int aInv = 0;
6161
int flag;
6262

@@ -75,13 +75,13 @@ static String decryptCipher(String cipher) {
7575
{here x is cipher[i] and m is 26} and added 'A'
7676
to bring it in the range of ASCII alphabet [65-90 | A-Z] */
7777
if (cipher.charAt(i) != ' ') {
78-
msg += (char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A');
78+
msg.append((char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A'));
7979
} else { // else simply append space character
80-
msg += cipher.charAt(i);
80+
msg.append(cipher.charAt(i));
8181
}
8282
}
8383

84-
return msg;
84+
return msg.toString();
8585
}
8686

8787
// Driver code

src/main/java/com/thealgorithms/ciphers/Blowfish.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ public class Blowfish {
10781078
* @return String object which is a binary representation of the hex number passed as parameter
10791079
*/
10801080
private String hexToBin(String hex) {
1081-
String binary = "";
1081+
StringBuilder binary = new StringBuilder();
10821082
long num;
10831083
String binary4B;
10841084
int n = hex.length();
@@ -1089,9 +1089,9 @@ private String hexToBin(String hex) {
10891089
binary4B = "0000" + binary4B;
10901090

10911091
binary4B = binary4B.substring(binary4B.length() - 4);
1092-
binary += binary4B;
1092+
binary.append(binary4B);
10931093
}
1094-
return binary;
1094+
return binary.toString();
10951095
}
10961096

10971097
/**
@@ -1103,12 +1103,12 @@ private String hexToBin(String hex) {
11031103
*/
11041104
private String binToHex(String binary) {
11051105
long num = Long.parseUnsignedLong(binary, 2);
1106-
String hex = Long.toHexString(num);
1106+
StringBuilder hex = new StringBuilder(Long.toHexString(num));
11071107
while (hex.length() < (binary.length() / 4)) {
1108-
hex = "0" + hex;
1108+
hex.insert(0, "0");
11091109
}
11101110

1111-
return hex;
1111+
return hex.toString();
11121112
}
11131113

11141114
/**
@@ -1121,12 +1121,12 @@ private String binToHex(String binary) {
11211121
private String xor(String a, String b) {
11221122
a = hexToBin(a);
11231123
b = hexToBin(b);
1124-
String ans = "";
1124+
StringBuilder ans = new StringBuilder();
11251125
for (int i = 0; i < a.length(); i++) {
1126-
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
1126+
ans.append((char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0'));
11271127
}
1128-
ans = binToHex(ans);
1129-
return ans;
1128+
ans = new StringBuilder(binToHex(ans.toString()));
1129+
return ans.toString();
11301130
}
11311131

11321132
/**

src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static String base2base(String n, int b1, int b2) {
136136
int decimalValue = 0;
137137
int charB2;
138138
char charB1;
139-
String output = "";
139+
StringBuilder output = new StringBuilder();
140140
// Go through every character of n
141141
for (int i = 0; i < n.length(); i++) {
142142
// store the character in charB1
@@ -167,15 +167,15 @@ public static String base2base(String n, int b1, int b2) {
167167
// If the remainder is a digit < 10, simply add it to
168168
// the left side of the new number.
169169
if (decimalValue % b2 < 10) {
170-
output = decimalValue % b2 + output;
170+
output.insert(0, decimalValue % b2);
171171
} // If the remainder is >= 10, add a character with the
172172
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
173173
else {
174-
output = (char) ((decimalValue % b2) + 55) + output;
174+
output.insert(0, (char) ((decimalValue % b2) + 55));
175175
}
176176
// Divide by the new base again
177177
decimalValue /= b2;
178178
}
179-
return output;
179+
return output.toString();
180180
}
181181
}

src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public String convert(String numHex) {
5252
*/
5353
public String completeDigits(String binNum) {
5454
final int byteSize = 8;
55-
while (binNum.length() < byteSize) {
56-
binNum = "0" + binNum;
55+
StringBuilder binNumBuilder = new StringBuilder(binNum);
56+
while (binNumBuilder.length() < byteSize) {
57+
binNumBuilder.insert(0, "0");
5758
}
59+
binNum = binNumBuilder.toString();
5860
return binNum;
5961
}
6062
}

src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public FloydWarshall(int numberofvertices) {
4242
public void floydwarshall(int[][] adjacencyMatrix) {
4343
// Initialize the distance matrix with the adjacency matrix.
4444
for (int source = 1; source <= numberofvertices; source++) {
45-
for (int destination = 1; destination <= numberofvertices; destination++) {
46-
distanceMatrix[source][destination] = adjacencyMatrix[source][destination];
47-
}
45+
System.arraycopy(adjacencyMatrix[source], 1, distanceMatrix[source], 1, numberofvertices);
4846
}
4947
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) {
5048
for (int source = 1; source <= numberofvertices; source++) {

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public static void main(String[] arg) {
403403
SinglyLinkedList list = new SinglyLinkedList();
404404
assert list.isEmpty();
405405
assert list.size() == 0 && list.count() == 0;
406-
assert list.toString().equals("");
406+
assert list.toString().isEmpty();
407407

408408
/* Test insert function */
409409
list.insertHead(5);

src/main/java/com/thealgorithms/datastructures/trees/GenericTree.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private static final class Node {
2323
}
2424

2525
private final Node root;
26+
2627
public GenericTree() { // Constructor
2728
Scanner scn = new Scanner(System.in);
2829
root = createTreeG(null, 0, scn);
@@ -225,8 +226,6 @@ private void removeleaves(Node node) {
225226
for (int i = 0; i < node.child.size(); i++) {
226227
if (node.child.get(i).child.size() == 0) {
227228
arr.add(i);
228-
// node.child.remove(i);
229-
// i--;
230229
} else {
231230
removeleaves(node.child.get(i));
232231
}

src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.thealgorithms.graph;
2+
13
import java.util.ArrayList;
24
import java.util.Arrays;
35
import java.util.HashMap;

src/main/java/com/thealgorithms/maths/NthUglyNumber.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.thealgorithms.maths;
22

3+
import static java.util.Collections.singletonList;
4+
35
import java.util.ArrayList;
4-
import java.util.Arrays;
56
import java.util.Map;
67
import org.apache.commons.lang3.tuple.MutablePair;
78

@@ -16,7 +17,7 @@
1617
* - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers
1718
*/
1819
public class NthUglyNumber {
19-
private ArrayList<Long> uglyNumbers = new ArrayList<>(Arrays.asList(1L));
20+
private ArrayList<Long> uglyNumbers = new ArrayList<>(singletonList(1L));
2021
private ArrayList<MutablePair<Integer, Integer>> positions = new ArrayList<>();
2122

2223
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy