Skip to content

Commit 49c7cef

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents d712f6f + 6a60458 commit 49c7cef

40 files changed

Lines changed: 373 additions & 126 deletions

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ jobs:
1717
if: >-
1818
github.event_name == 'pull_request' &&
1919
github.event.pull_request.head.repo.full_name != github.repository
20-
uses: codecov/codecov-action@v4
20+
uses: codecov/codecov-action@v5
2121
with:
2222
fail_ci_if_error: true
2323
- name: Upload coverage to codecov (with token)
2424
if: >
2525
github.repository == 'TheAlgorithms/Java' &&
2626
(github.event_name != 'pull_request' ||
2727
github.event.pull_request.head.repo.full_name == github.repository)
28-
uses: codecov/codecov-action@v4
28+
uses: codecov/codecov-action@v5
2929
with:
3030
token: ${{ secrets.CODECOV_TOKEN }}
3131
fail_ci_if_error: true

.gitpod.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gitpod/workspace-java-21:2024-09-11-00-04-27
1+
FROM gitpod/workspace-java-21:2024-11-26-08-43-19
22

33
ENV LLVM_SCRIPT="tmp_llvm.sh"
44

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<maven.compiler.source>21</maven.compiler.source>
1414
<maven.compiler.target>21</maven.compiler.target>
15-
<assertj.version>3.26.3</assertj.version>
15+
<assertj.version>3.27.0</assertj.version>
1616
</properties>
1717

1818
<dependencyManagement>
1919
<dependencies>
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.3</version>
23+
<version>5.11.4</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.3</version>
34+
<version>5.11.4</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>org.junit.jupiter</groupId>
5353
<artifactId>junit-jupiter-api</artifactId>
54-
<version>5.11.3</version>
54+
<version>5.11.4</version>
5555
<scope>test</scope>
5656
</dependency>
5757
<dependency>
@@ -62,15 +62,15 @@
6262
<dependency>
6363
<groupId>org.apache.commons</groupId>
6464
<artifactId>commons-collections4</artifactId>
65-
<version>4.5.0-M2</version>
65+
<version>4.5.0-M3</version>
6666
</dependency>
6767
</dependencies>
6868

6969
<build>
7070
<plugins>
7171
<plugin>
7272
<artifactId>maven-surefire-plugin</artifactId>
73-
<version>3.5.1</version>
73+
<version>3.5.2</version>
7474
<configuration>
7575
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
7676
</configuration>
@@ -125,22 +125,22 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.20.0</version>
128+
<version>10.21.1</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>
132132
<plugin>
133133
<groupId>com.github.spotbugs</groupId>
134134
<artifactId>spotbugs-maven-plugin</artifactId>
135-
<version>4.8.6.5</version>
135+
<version>4.8.6.6</version>
136136
<configuration>
137137
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
138138
<includeTests>true</includeTests>
139139
<plugins>
140140
<plugin>
141141
<groupId>com.mebigfatguy.fb-contrib</groupId>
142142
<artifactId>fb-contrib</artifactId>
143-
<version>7.6.5</version>
143+
<version>7.6.9</version>
144144
</plugin>
145145
<plugin>
146146
<groupId>com.h3xstream.findsecbugs</groupId>

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
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.Arrays;
4+
import java.util.Set;
5+
import java.util.TreeSet;
6+
import org.apache.commons.lang3.tuple.Pair;
7+
8+
/**
9+
* Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
10+
*/
11+
public class DijkstraOptimizedAlgorithm {
12+
13+
private final int vertexCount;
14+
15+
/**
16+
* Constructs a Dijkstra object with the given number of vertices.
17+
*
18+
* @param vertexCount The number of vertices in the graph.
19+
*/
20+
public DijkstraOptimizedAlgorithm(int vertexCount) {
21+
this.vertexCount = vertexCount;
22+
}
23+
24+
/**
25+
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
26+
*
27+
* The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}
28+
* to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.
29+
*
30+
* @param graph The graph represented as an adjacency matrix.
31+
* @param source The source vertex.
32+
* @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.
33+
* @throws IllegalArgumentException if the source vertex is out of range.
34+
*/
35+
public int[] run(int[][] graph, int source) {
36+
if (source < 0 || source >= vertexCount) {
37+
throw new IllegalArgumentException("Incorrect source");
38+
}
39+
40+
int[] distances = new int[vertexCount];
41+
boolean[] processed = new boolean[vertexCount];
42+
Set<Pair<Integer, Integer>> unprocessed = new TreeSet<>();
43+
44+
Arrays.fill(distances, Integer.MAX_VALUE);
45+
Arrays.fill(processed, false);
46+
distances[source] = 0;
47+
unprocessed.add(Pair.of(0, source));
48+
49+
while (!unprocessed.isEmpty()) {
50+
Pair<Integer, Integer> distanceAndU = unprocessed.iterator().next();
51+
unprocessed.remove(distanceAndU);
52+
int u = distanceAndU.getRight();
53+
processed[u] = true;
54+
55+
for (int v = 0; v < vertexCount; v++) {
56+
if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
57+
unprocessed.remove(Pair.of(distances[v], v));
58+
distances[v] = distances[u] + graph[u][v];
59+
unprocessed.add(Pair.of(distances[v], v));
60+
}
61+
}
62+
}
63+
64+
return distances;
65+
}
66+
}

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++) {

0 commit comments

Comments
 (0)