Skip to content

Commit 89fc5bd

Browse files
Merge branch 'master' into master
2 parents 8820a15 + ebc3cd2 commit 89fc5bd

File tree

14 files changed

+408
-76
lines changed

14 files changed

+408
-76
lines changed

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
* [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java)
9595
* [IntegerToEnglish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java)
9696
* [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java)
97+
* [IPConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPConverter.java)
9798
* [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
9899
* [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
99100
* [OctalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java)
@@ -598,7 +599,9 @@
598599
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
599600
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
600601
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
602+
* [PostfixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java)
601603
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
604+
* [PrefixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixEvaluator.java)
602605
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
603606
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
604607
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
@@ -725,6 +728,7 @@
725728
* [HexToOctTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.java)
726729
* [IntegerToEnglishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java)
727730
* [IntegerToRomanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java)
731+
* [IPConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPConverterTest.java)
728732
* [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java)
729733
* [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java)
730734
* [OctalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToHexadecimalTest.java)
@@ -1128,7 +1132,9 @@
11281132
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
11291133
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
11301134
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
1135+
* [PostfixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java)
11311136
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
1137+
* [PrefixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java)
11321138
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
11331139
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
11341140
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)

src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,80 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
55
*
6-
* Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
6+
* BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
77
*
88
* For more information, refer to the
99
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
1010
*
1111
* <b>Example usage:</b>
1212
* <pre>
13-
* int binary = BcdConversion.bcdToBinary(0x1234);
14-
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
13+
* int decimal = BcdConversion.bcdToDecimal(0x1234);
14+
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
1515
*
16-
* int bcd = BcdConversion.binaryToBcd(1234);
17-
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
16+
* int bcd = BcdConversion.decimalToBcd(1234);
17+
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
1818
* </pre>
1919
*/
2020
public final class BcdConversion {
2121
private BcdConversion() {
2222
}
23+
2324
/**
24-
* Converts a BCD (Binary-Coded Decimal) number to binary.
25+
* Converts a BCD (Binary-Coded Decimal) number to a decimal number.
26+
* <p>Steps:
27+
* <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
28+
* <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
29+
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
30+
* <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
31+
* <p>5. Repeat steps 1-4 until the BCD number is zero.
2532
*
2633
* @param bcd The BCD number.
27-
* @return The corresponding binary number.
34+
* @return The corresponding decimal number.
35+
* @throws IllegalArgumentException if the BCD number contains invalid digits.
2836
*/
29-
public static int bcdToBinary(int bcd) {
30-
int binary = 0;
37+
public static int bcdToDecimal(int bcd) {
38+
int decimal = 0;
3139
int multiplier = 1;
40+
41+
// Validate BCD digits
3242
while (bcd > 0) {
33-
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
34-
binary += digit * multiplier;
43+
int digit = bcd & 0xF;
44+
if (digit > 9) {
45+
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
46+
}
47+
decimal += digit * multiplier;
3548
multiplier *= 10;
36-
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
49+
bcd >>= 4;
3750
}
38-
return binary;
51+
return decimal;
3952
}
4053

4154
/**
42-
* Converts a binary number to BCD (Binary-Coded Decimal).
55+
* Converts a decimal number to BCD (Binary-Coded Decimal).
56+
* <p>Steps:
57+
* <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
58+
* <p>2. Extract the last decimal digit from the decimal number.
59+
* <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
60+
* <p>4. Remove the last decimal digit from the decimal number.
61+
* <p>5. Repeat steps 2-4 until the decimal number is zero.
4362
*
44-
* @param binary The binary number.
63+
* @param decimal The decimal number.
4564
* @return The corresponding BCD number.
65+
* @throws IllegalArgumentException if the decimal number is greater than 9999.
4666
*/
47-
public static int binaryToBcd(int binary) {
67+
public static int decimalToBcd(int decimal) {
68+
if (decimal < 0 || decimal > 9999) {
69+
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
70+
}
71+
4872
int bcd = 0;
4973
int shift = 0;
50-
while (binary > 0) {
51-
int digit = binary % 10; // Extract the last decimal digit
52-
bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
53-
binary /= 10; // Remove the last decimal digit
74+
while (decimal > 0) {
75+
int digit = decimal % 10;
76+
bcd |= (digit << (shift * 4));
77+
decimal /= 10;
5478
shift++;
5579
}
5680
return bcd;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* Converts an IPv4 address to its binary equivalent and vice-versa.
5+
* IP to Binary: Converts an IPv4 address to its binary equivalent.
6+
* Example: 127.3.4.5 -> 01111111.00000011.00000100.00000101
7+
*
8+
* Binary to IP: Converts a binary equivalent to an IPv4 address.
9+
* Example: 01111111.00000011.00000100.00000101 -> 127.3.4.5
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class IPConverter {
14+
private IPConverter() {
15+
}
16+
17+
/**
18+
* Converts an IPv4 address to its binary equivalent.
19+
* @param ip The IPv4 address to convert.
20+
* @return The binary equivalent of the IPv4 address.
21+
*/
22+
public static String ipToBinary(String ip) {
23+
StringBuilder binary = new StringBuilder();
24+
for (String octet : ip.split("\\.")) {
25+
binary.append(octetToBinary(Integer.parseInt(octet))).append(".");
26+
}
27+
return binary.substring(0, binary.length() - 1);
28+
}
29+
30+
/**
31+
* Converts a single octet to its 8-bit binary representation.
32+
* @param octet The octet to convert (0-255).
33+
* @return The 8-bit binary representation as a String.
34+
*/
35+
private static String octetToBinary(int octet) {
36+
char[] binary = {'0', '0', '0', '0', '0', '0', '0', '0'};
37+
for (int i = 7; i >= 0; i--) {
38+
if ((octet & 1) == 1) {
39+
binary[i] = '1';
40+
}
41+
octet >>>= 1;
42+
}
43+
return new String(binary);
44+
}
45+
46+
/**
47+
* Converts a binary equivalent to an IPv4 address.
48+
* @param binary The binary equivalent to convert.
49+
* @return The IPv4 address of the binary equivalent.
50+
*/
51+
public static String binaryToIP(String binary) {
52+
StringBuilder ip = new StringBuilder();
53+
for (String octet : binary.split("\\.")) {
54+
ip.append(Integer.parseInt(octet, 2)).append(".");
55+
}
56+
return ip.substring(0, ip.length() - 1);
57+
}
58+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
class Cycle {
77

88
private final int nodes;
9-
private final int edges;
109
private int[][] adjacencyMatrix;
1110
private boolean[] visited;
1211
ArrayList<ArrayList<Integer>> cycles = new ArrayList<ArrayList<Integer>>();
@@ -16,7 +15,7 @@ class Cycle {
1615
System.out.print("Enter the no. of nodes: ");
1716
nodes = in.nextInt();
1817
System.out.print("Enter the no. of Edges: ");
19-
edges = in.nextInt();
18+
final int edges = in.nextInt();
2019

2120
adjacencyMatrix = new int[nodes][nodes];
2221
visited = new boolean[nodes];

src/main/java/com/thealgorithms/others/CRCAlgorithm.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public class CRCAlgorithm {
2525

2626
private ArrayList<Integer> message;
2727

28-
private ArrayList<Integer> dividedMessage;
29-
3028
private ArrayList<Integer> p;
3129

3230
private Random randomGenerator;
@@ -44,7 +42,6 @@ public CRCAlgorithm(String str, int size, double ber) {
4442
messageChanged = false;
4543
message = new ArrayList<>();
4644
messSize = size;
47-
dividedMessage = new ArrayList<>();
4845
p = new ArrayList<>();
4946
for (int i = 0; i < str.length(); i++) {
5047
p.add(Character.getNumericValue(str.charAt(i)));
@@ -103,7 +100,6 @@ public int getCorrectMess() {
103100
public void refactor() {
104101
messageChanged = false;
105102
message = new ArrayList<>();
106-
dividedMessage = new ArrayList<>();
107103
}
108104

109105
/**
@@ -156,7 +152,7 @@ public void divideMessageWithP(boolean check) {
156152
}
157153
}
158154
}
159-
dividedMessage = (ArrayList<Integer>) x.clone();
155+
ArrayList<Integer> dividedMessage = (ArrayList<Integer>) x.clone();
160156
if (!check) {
161157
message.addAll(dividedMessage);
162158
} else {

src/main/java/com/thealgorithms/sorts/LinkListSort.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,12 @@ static int count(Node head) {
259259

260260
class Task2 {
261261

262-
private int[] a;
263-
264262
public Node sortByHeapSort(Node head) {
265263
if (head == null || head.next == null) {
266264
return head;
267265
}
268266
int c = count(head);
269-
a = new int[c];
267+
int[] a = new int[c];
270268
// Array of size c is created
271269
int i = 0;
272270
for (Node ptr = head; ptr != null; ptr = ptr.next) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Set;
4+
import java.util.Stack;
5+
6+
/**
7+
* Evaluate a postfix (Reverse Polish) expression using a stack.
8+
*
9+
* <p>Example: Expression "5 6 + 2 *" results in 22.
10+
* <p>Applications: Used in calculators and expression evaluation in compilers.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class PostfixEvaluator {
15+
private PostfixEvaluator() {
16+
}
17+
18+
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
19+
20+
/**
21+
* Evaluates the given postfix expression and returns the result.
22+
*
23+
* @param expression The postfix expression as a string with operands and operators separated by spaces.
24+
* @return The result of evaluating the postfix expression.
25+
* @throws IllegalArgumentException if the expression is invalid.
26+
*/
27+
public static int evaluatePostfix(String expression) {
28+
Stack<Integer> stack = new Stack<>();
29+
30+
for (String token : expression.split("\\s+")) {
31+
if (isOperator(token)) {
32+
int operand2 = stack.pop();
33+
int operand1 = stack.pop();
34+
stack.push(applyOperator(token, operand1, operand2));
35+
} else {
36+
stack.push(Integer.valueOf(token));
37+
}
38+
}
39+
40+
if (stack.size() != 1) {
41+
throw new IllegalArgumentException("Invalid expression");
42+
}
43+
44+
return stack.pop();
45+
}
46+
47+
/**
48+
* Checks if the given token is an operator.
49+
*
50+
* @param token The token to check.
51+
* @return true if the token is an operator, false otherwise.
52+
*/
53+
private static boolean isOperator(String token) {
54+
return OPERATORS.contains(token);
55+
}
56+
57+
/**
58+
* Applies the given operator to the two operands.
59+
*
60+
* @param operator The operator to apply.
61+
* @param a The first operand.
62+
* @param b The second operand.
63+
* @return The result of applying the operator to the operands.
64+
*/
65+
private static int applyOperator(String operator, int a, int b) {
66+
return switch (operator) {
67+
case "+" -> a + b;
68+
case "-" -> a - b;
69+
case "*" -> a * b;
70+
case "/" -> a / b;
71+
default -> throw new IllegalArgumentException("Invalid operator");
72+
};
73+
}
74+
}

0 commit comments

Comments
 (0)