Skip to content

Commit df0c997

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

29 files changed

+92
-75
lines changed

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

-2
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

+8-8
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

+10-10
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

+4-4
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

+4-2
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

+1-3
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

+1-1
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

+1-2
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

+2
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

+3-2
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
/**

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ static void test(int startValue, int stopValue) {
3333
// System.out.println(i+ " "+ j);
3434
if (isVampireNumber(i, j, true)) {
3535
countofRes++;
36-
res.append("" + countofRes + ": = ( " + i + "," + j + " = " + i * j + ")"
37-
+ "\n");
36+
res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n");
3837
}
3938
}
4039
}

src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.scheduling;
22

3+
import java.util.Collections;
34
import java.util.LinkedList;
45
import java.util.PriorityQueue;
56
import java.util.Queue;
@@ -72,9 +73,7 @@ public static Process[] scheduleProcesses(Process[] processes) {
7273
int index = 0;
7374
Process[] executionOrder = new Process[processes.length];
7475

75-
for (Process process : processes) {
76-
waitingQueue.add(process);
77-
}
76+
Collections.addAll(waitingQueue, processes);
7877

7978
while (!waitingQueue.isEmpty() || !pq.isEmpty()) {
8079
// Add processes that have arrived to the priority queue

src/test/java/com/thealgorithms/backtracking/NQueensTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.backtracking;
22

3+
import static java.util.Collections.singletonList;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45

56
import java.util.ArrayList;
@@ -11,7 +12,7 @@ public class NQueensTest {
1112

1213
@Test
1314
public void testNQueens1() {
14-
List<List<String>> expected = Arrays.asList(Arrays.asList("Q"));
15+
List<List<String>> expected = singletonList(singletonList("Q"));
1516
assertEquals(expected, NQueens.getNQueensArrangements(1));
1617
}
1718

src/test/java/com/thealgorithms/bitmanipulation/GenerateSubsetsTest.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.bitmanipulation;
22

3+
import static java.util.Collections.singletonList;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45

56
import java.util.ArrayList;
@@ -14,8 +15,8 @@ void testGenerateSubsetsWithTwoElements() {
1415
int[] set = {1, 2};
1516
List<List<Integer>> expected = new ArrayList<>();
1617
expected.add(new ArrayList<>());
17-
expected.add(Arrays.asList(1));
18-
expected.add(Arrays.asList(2));
18+
expected.add(singletonList(1));
19+
expected.add(singletonList(2));
1920
expected.add(Arrays.asList(1, 2));
2021

2122
List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
@@ -27,7 +28,7 @@ void testGenerateSubsetsWithOneElement() {
2728
int[] set = {3};
2829
List<List<Integer>> expected = new ArrayList<>();
2930
expected.add(new ArrayList<>());
30-
expected.add(Arrays.asList(3));
31+
expected.add(singletonList(3));
3132

3233
List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
3334
assertEquals(expected, result);
@@ -38,10 +39,10 @@ void testGenerateSubsetsWithThreeElements() {
3839
int[] set = {4, 5, 6};
3940
List<List<Integer>> expected = new ArrayList<>();
4041
expected.add(new ArrayList<>());
41-
expected.add(Arrays.asList(4));
42-
expected.add(Arrays.asList(5));
42+
expected.add(singletonList(4));
43+
expected.add(singletonList(5));
4344
expected.add(Arrays.asList(4, 5));
44-
expected.add(Arrays.asList(6));
45+
expected.add(singletonList(6));
4546
expected.add(Arrays.asList(4, 6));
4647
expected.add(Arrays.asList(5, 6));
4748
expected.add(Arrays.asList(4, 5, 6));

src/test/java/com/thealgorithms/datastructures/graphs/AStarTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.datastructures.graphs;
22

3+
import static java.util.Collections.singletonList;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56

@@ -41,6 +42,6 @@ public void testAStarPathNotFound() {
4142
public void testAStarSameNode() {
4243
AStar.PathAndDistance result = AStar.aStar(0, 0, graph, heuristic);
4344
assertEquals(0, result.getDistance(), "Expected distance from 0 to 0 is 0");
44-
assertEquals(Arrays.asList(0), result.getPath(), "Expected path should only contain the start node");
45+
assertEquals(singletonList(0), result.getPath(), "Expected path should only contain the start node");
4546
}
4647
}

src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.dynamicprogramming;
22

3+
import static java.util.Collections.emptyList;
4+
import static java.util.Collections.singletonList;
35
import static org.junit.jupiter.api.Assertions.assertEquals;
46

57
import java.util.Arrays;
@@ -10,7 +12,7 @@ public class AllConstructTest {
1012

1113
@Test
1214
public void testAllConstructBasic() {
13-
List<List<String>> expected = Arrays.asList(Arrays.asList("he", "l", "l", "o"));
15+
List<List<String>> expected = singletonList(Arrays.asList("he", "l", "l", "o"));
1416
List<List<String>> result = AllConstruct.allConstruct("hello", Arrays.asList("he", "l", "o"));
1517
assertEquals(expected, result);
1618
}
@@ -24,14 +26,14 @@ public void testAllConstructMultipleWays() {
2426

2527
@Test
2628
public void testAllConstructNoWays() {
27-
List<List<String>> expected = Arrays.asList();
29+
List<List<String>> expected = emptyList();
2830
List<List<String>> result = AllConstruct.allConstruct("abcdef", Arrays.asList("gh", "ijk"));
2931
assertEquals(expected, result);
3032
}
3133

3234
@Test
3335
public void testAllConstructEmptyTarget() {
34-
List<List<String>> expected = Arrays.asList(Arrays.asList());
36+
List<List<String>> expected = singletonList(emptyList());
3537
List<List<String>> result = AllConstruct.allConstruct("", Arrays.asList("a", "b", "c"));
3638
assertEquals(expected, result);
3739
}

src/test/java/com/thealgorithms/dynamicprogramming/AssignmentUsingBitmaskTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.dynamicprogramming;
22

3+
import static java.util.Collections.singletonList;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45

56
import java.util.Arrays;
@@ -23,7 +24,7 @@ public void testCountNoOfWays() {
2324
public void testNoPossibleAssignments() {
2425
int totalTasks = 3;
2526

26-
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(2), Arrays.asList(3));
27+
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(2), singletonList(3));
2728

2829
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
2930
int ways = assignment.countNoOfWays();
@@ -34,7 +35,7 @@ public void testNoPossibleAssignments() {
3435
public void testSinglePersonMultipleTasks() {
3536
int totalTasks = 3;
3637

37-
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1, 2, 3));
38+
List<List<Integer>> taskPerformed = singletonList(Arrays.asList(1, 2, 3));
3839

3940
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
4041
int ways = assignment.countNoOfWays();
@@ -45,7 +46,7 @@ public void testSinglePersonMultipleTasks() {
4546
public void testMultiplePeopleSingleTask() {
4647
int totalTasks = 1;
4748

48-
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1), Arrays.asList(1));
49+
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(1), singletonList(1));
4950

5051
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
5152
int ways = assignment.countNoOfWays();

src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.thealgorithms.graph;
2+
13
import static org.junit.jupiter.api.Assertions.assertEquals;
24

35
import java.util.ArrayList;

0 commit comments

Comments
 (0)