Skip to content

Commit 7695905

Browse files
Merge branch 'master' into patch-1
2 parents e863f65 + eecec0f commit 7695905

21 files changed

+404
-208
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
44
- [ ] This pull request is all my own work -- I have not plagiarized it.
5-
- [ ] I know that pull requests will not be merged if they fail the automated tests.
6-
- [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
7-
- [ ] All new Java files are placed inside an existing directory.
8-
- [ ] All filenames are in all uppercase characters with no spaces or dashes.
5+
- [ ] All filenames are in PascalCase.
96
- [ ] All functions and variable names follow Java naming conventions.
107
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
11-
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.

src/main/java/com/thealgorithms/backtracking/FloodFill.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class FloodFill {
88

99
/**
10-
* Get the color at the given co-odrinates of a 2D image
10+
* Get the color at the given coordinates of a 2D image
1111
*
1212
* @param image The image to be filled
1313
* @param x The x co-ordinate of which color is to be obtained
@@ -19,9 +19,9 @@ public static int getPixel(int[][] image, int x, int y) {
1919
}
2020

2121
/**
22-
* Put the color at the given co-odrinates of a 2D image
22+
* Put the color at the given coordinates of a 2D image
2323
*
24-
* @param image The image to be filed
24+
* @param image The image to be filled
2525
* @param x The x co-ordinate at which color is to be filled
2626
* @param y The y co-ordinate at which color is to be filled
2727
*/
@@ -32,7 +32,7 @@ public static void putPixel(int[][] image, int x, int y, int newColor) {
3232
/**
3333
* Fill the 2D image with new color
3434
*
35-
* @param image The image to be filed
35+
* @param image The image to be filled
3636
* @param x The x co-ordinate at which color is to be filled
3737
* @param y The y co-ordinate at which color is to be filled
3838
* @param newColor The new color which to be filled in the image
Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.ciphers;
22

3-
import java.util.Scanner;
4-
53
/**
64
* A Java implementation of Caesar Cipher. /It is a type of substitution cipher
75
* in which each letter in the plaintext is replaced by a letter some fixed
@@ -18,7 +16,7 @@ public class Caesar {
1816
*
1917
* @return Encrypted message
2018
*/
21-
public static String encode(String message, int shift) {
19+
public String encode(String message, int shift) {
2220
StringBuilder encoded = new StringBuilder();
2321

2422
shift %= 26;
@@ -29,10 +27,10 @@ public static String encode(String message, int shift) {
2927
// is in-order latin alphabet
3028
char current = message.charAt(i); // Java law : char + int = char
3129

32-
if (IsCapitalLatinLetter(current)) {
30+
if (isCapitalLatinLetter(current)) {
3331
current += shift;
3432
encoded.append((char) (current > 'Z' ? current - 26 : current)); // 26 = number of latin letters
35-
} else if (IsSmallLatinLetter(current)) {
33+
} else if (isSmallLatinLetter(current)) {
3634
current += shift;
3735
encoded.append((char) (current > 'z' ? current - 26 : current)); // 26 = number of latin letters
3836
} else {
@@ -48,18 +46,18 @@ public static String encode(String message, int shift) {
4846
*
4947
* @return message
5048
*/
51-
public static String decode(String encryptedMessage, int shift) {
49+
public String decode(String encryptedMessage, int shift) {
5250
StringBuilder decoded = new StringBuilder();
5351

5452
shift %= 26;
5553

5654
final int length = encryptedMessage.length();
5755
for (int i = 0; i < length; i++) {
5856
char current = encryptedMessage.charAt(i);
59-
if (IsCapitalLatinLetter(current)) {
57+
if (isCapitalLatinLetter(current)) {
6058
current -= shift;
6159
decoded.append((char) (current < 'A' ? current + 26 : current)); // 26 = number of latin letters
62-
} else if (IsSmallLatinLetter(current)) {
60+
} else if (isSmallLatinLetter(current)) {
6361
current -= shift;
6462
decoded.append((char) (current < 'a' ? current + 26 : current)); // 26 = number of latin letters
6563
} else {
@@ -72,69 +70,26 @@ public static String decode(String encryptedMessage, int shift) {
7270
/**
7371
* @return true if character is capital Latin letter or false for others
7472
*/
75-
private static boolean IsCapitalLatinLetter(char c) {
73+
private static boolean isCapitalLatinLetter(char c) {
7674
return c >= 'A' && c <= 'Z';
7775
}
7876

7977
/**
8078
* @return true if character is small Latin letter or false for others
8179
*/
82-
private static boolean IsSmallLatinLetter(char c) {
80+
private static boolean isSmallLatinLetter(char c) {
8381
return c >= 'a' && c <= 'z';
8482
}
8583

8684
/**
8785
* @return string array which contains all the possible decoded combination.
8886
*/
89-
public static String[] bruteforce(String encryptedMessage) {
87+
public String[] bruteforce(String encryptedMessage) {
9088
String[] listOfAllTheAnswers = new String[27];
9189
for (int i = 0; i <= 26; i++) {
9290
listOfAllTheAnswers[i] = decode(encryptedMessage, i);
9391
}
9492

9593
return listOfAllTheAnswers;
9694
}
97-
98-
public static void main(String[] args) {
99-
Scanner input = new Scanner(System.in);
100-
int shift = 0;
101-
System.out.println("Please enter the message (Latin Alphabet)");
102-
String message = input.nextLine();
103-
System.out.println(message);
104-
System.out.println("(E)ncode or (D)ecode or (B)ruteforce?");
105-
char choice = input.next().charAt(0);
106-
switch (choice) {
107-
case 'E':
108-
case 'e':
109-
System.out.println("Please enter the shift number");
110-
shift = input.nextInt() % 26;
111-
System.out.println(
112-
"ENCODED MESSAGE IS \n" + encode(message, shift)
113-
); // send our function to handle
114-
break;
115-
case 'D':
116-
case 'd':
117-
System.out.println("Please enter the shift number");
118-
shift = input.nextInt() % 26;
119-
System.out.println(
120-
"DECODED MESSAGE IS \n" + decode(message, shift)
121-
);
122-
break;
123-
case 'B':
124-
case 'b':
125-
String[] listOfAllTheAnswers = bruteforce(message);
126-
for (int i = 0; i <= 26; i++) {
127-
System.out.println(
128-
"FOR SHIFT " +
129-
String.valueOf(i) +
130-
" decoded message is " +
131-
listOfAllTheAnswers[i]
132-
);
133-
}
134-
default:
135-
System.out.println("default case");
136-
}
137-
138-
input.close();
139-
}
14095
}

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,15 @@
22

33
import java.math.BigInteger;
44
import java.security.SecureRandom;
5-
import javax.swing.*;
65

76
/**
87
* @author Nguyen Duy Tiep on 23-Oct-17.
98
*/
10-
public final class RSA {
9+
public class RSA {
1110

12-
public static void main(String[] args) {
13-
RSA rsa = new RSA(1024);
14-
String text1 = JOptionPane.showInputDialog(
15-
"Enter a message to encrypt :"
16-
);
17-
18-
String ciphertext = rsa.encrypt(text1);
19-
JOptionPane.showMessageDialog(
20-
null,
21-
"Your encrypted message : " + ciphertext
22-
);
23-
24-
JOptionPane.showMessageDialog(
25-
null,
26-
"Your message after decrypt : " + rsa.decrypt(ciphertext)
27-
);
28-
}
29-
30-
private BigInteger modulus, privateKey, publicKey;
11+
private BigInteger modulus;
12+
private BigInteger privateKey;
13+
private BigInteger publicKey;
3114

3215
public RSA(int bits) {
3316
generateKeys(bits);
@@ -77,10 +60,10 @@ public synchronized void generateKeys(int bits) {
7760
BigInteger m =
7861
(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
7962

80-
publicKey = new BigInteger("3");
63+
publicKey = BigInteger.valueOf(3L);
8164

8265
while (m.gcd(publicKey).intValue() > 1) {
83-
publicKey = publicKey.add(new BigInteger("2"));
66+
publicKey = publicKey.add(BigInteger.valueOf(2L));
8467
}
8568

8669
privateKey = publicKey.modInverse(m);

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
*/
99
public class Vigenere {
1010

11-
public static String encrypt(final String message, final String key) {
11+
public String encrypt(final String message, final String key) {
1212
StringBuilder result = new StringBuilder();
1313

14-
for (int i = 0, j = 0; i < message.length(); i++) {
14+
int j = 0;
15+
for (int i = 0; i < message.length(); i++) {
1516
char c = message.charAt(i);
1617
if (Character.isLetter(c)) {
1718
if (Character.isUpperCase(c)) {
@@ -39,10 +40,11 @@ public static String encrypt(final String message, final String key) {
3940
return result.toString();
4041
}
4142

42-
public static String decrypt(final String message, final String key) {
43+
public String decrypt(final String message, final String key) {
4344
StringBuilder result = new StringBuilder();
4445

45-
for (int i = 0, j = 0; i < message.length(); i++) {
46+
int j = 0;
47+
for (int i = 0; i < message.length(); i++) {
4648
char c = message.charAt(i);
4749
if (Character.isLetter(c)) {
4850
if (Character.isUpperCase(c)) {
@@ -66,13 +68,4 @@ public static String decrypt(final String message, final String key) {
6668
}
6769
return result.toString();
6870
}
69-
70-
public static void main(String[] args) {
71-
String text = "Hello World!";
72-
String key = "itsakey";
73-
System.out.println(text);
74-
String ciphertext = encrypt(text, key);
75-
System.out.println(ciphertext);
76-
System.out.println(decrypt(ciphertext, key));
77-
}
7871
}
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
package com.thealgorithms.divideandconquer;
22

3-
public class BinaryExponentiation {
3+
// Java Program to Implement Binary Exponentiation (power in log n)
44

5-
public static void main(String args[]) {
6-
System.out.println(calculatePower(2, 30));
7-
}
5+
/*
6+
* Binary Exponentiation is a method to calculate a to the power of b.
7+
* It is used to calculate a^n in O(log n) time.
8+
*
9+
* Reference:
10+
* https://iq.opengenus.org/binary-exponentiation/
11+
*/
12+
13+
public class BinaryExponentiation {
814

9-
// Function to calculate x^y
10-
// Time Complexity: O(logn)
15+
// recursive function to calculate a to the power of b
1116
public static long calculatePower(long x, long y) {
1217
if (y == 0) {
1318
return 1;
1419
}
1520
long val = calculatePower(x, y / 2);
16-
val *= val;
17-
if (y % 2 == 1) {
18-
val *= x;
21+
if (y % 2 == 0) {
22+
return val * val;
23+
}
24+
return val * val * x;
25+
}
26+
27+
// iterative function to calculate a to the power of b
28+
long power(long N, long M) {
29+
long power = N, sum = 1;
30+
while (M > 0) {
31+
if ((M & 1) == 1) {
32+
sum *= power;
33+
}
34+
power = power * power;
35+
M = M >> 1;
1936
}
20-
return val;
37+
return sum;
2138
}
2239
}

0 commit comments

Comments
 (0)