Skip to content

Commit be4c65f

Browse files
authored
Merge branch 'master' into thombergs-patch-7
2 parents 369d471 + a08b07b commit be4c65f

222 files changed

Lines changed: 5574 additions & 557 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
6262
**/dist
6363
**/tmp
6464
**/out-tsc
65+
**/nbproject/
66+
**/nb-configuration.xml

‎.travis.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ before_install:
44
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
55

66
install: skip
7-
script: travis_wait 60 mvn -q install -Pdefault
7+
script: travis_wait 60 mvn -q install -Pdefault-first,default-second
88

99
sudo: required
1010

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
2121
Building the project
2222
====================
2323
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
24+
25+
26+
Building a single module
27+
====================
28+
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory

‎algorithms/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@
2828
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
2929
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
3030
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
31+
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
32+
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
33+
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)

‎algorithms/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<artifactId>commons-math3</artifactId>
1818
<version>${commons-math3.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>commons-codec</groupId>
22+
<artifactId>commons-codec</artifactId>
23+
<version>${commons-codec.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.projectlombok</groupId>
2227
<artifactId>lombok</artifactId>
@@ -85,6 +90,7 @@
8590
<io.jenetics.version>3.7.0</io.jenetics.version>
8691
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
8792
<org.assertj.core.version>3.9.0</org.assertj.core.version>
93+
<commons-codec.version>1.11</commons-codec.version>
8894
</properties>
8995

9096
</project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.algorithms.conversion;
2+
3+
import java.math.BigInteger;
4+
5+
import javax.xml.bind.DatatypeConverter;
6+
7+
import org.apache.commons.codec.DecoderException;
8+
import org.apache.commons.codec.binary.Hex;
9+
10+
import com.google.common.io.BaseEncoding;
11+
12+
public class HexStringConverter {
13+
14+
/**
15+
* Create a byte Array from String of hexadecimal digits using Character conversion
16+
* @param hexString - Hexadecimal digits as String
17+
* @return Desired byte Array
18+
*/
19+
public byte[] decodeHexString(String hexString) {
20+
if (hexString.length() % 2 == 1) {
21+
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
22+
}
23+
byte[] bytes = new byte[hexString.length() / 2];
24+
25+
for (int i = 0; i < hexString.length(); i += 2) {
26+
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
27+
}
28+
return bytes;
29+
}
30+
31+
/**
32+
* Create a String of hexadecimal digits from a byte Array using Character conversion
33+
* @param byteArray - The byte Array
34+
* @return Desired String of hexadecimal digits in lower case
35+
*/
36+
public String encodeHexString(byte[] byteArray) {
37+
StringBuffer hexStringBuffer = new StringBuffer();
38+
for (int i = 0; i < byteArray.length; i++) {
39+
hexStringBuffer.append(byteToHex(byteArray[i]));
40+
}
41+
return hexStringBuffer.toString();
42+
}
43+
44+
public String byteToHex(byte num) {
45+
char[] hexDigits = new char[2];
46+
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
47+
hexDigits[1] = Character.forDigit((num & 0xF), 16);
48+
return new String(hexDigits);
49+
}
50+
51+
public byte hexToByte(String hexString) {
52+
int firstDigit = toDigit(hexString.charAt(0));
53+
int secondDigit = toDigit(hexString.charAt(1));
54+
return (byte) ((firstDigit << 4) + secondDigit);
55+
}
56+
57+
private int toDigit(char hexChar) {
58+
int digit = Character.digit(hexChar, 16);
59+
if(digit == -1) {
60+
throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar);
61+
}
62+
return digit;
63+
}
64+
65+
public String encodeUsingBigIntegerToString(byte[] bytes) {
66+
BigInteger bigInteger = new BigInteger(1, bytes);
67+
return bigInteger.toString(16);
68+
}
69+
70+
public String encodeUsingBigIntegerStringFormat(byte[] bytes) {
71+
BigInteger bigInteger = new BigInteger(1, bytes);
72+
return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
73+
}
74+
75+
public byte[] decodeUsingBigInteger(String hexString) {
76+
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
77+
if (byteArray[0] == 0) {
78+
byte[] output = new byte[byteArray.length - 1];
79+
System.arraycopy(byteArray, 1, output, 0, output.length);
80+
return output;
81+
}
82+
return byteArray;
83+
}
84+
85+
public String encodeUsingDataTypeConverter(byte[] bytes) {
86+
return DatatypeConverter.printHexBinary(bytes);
87+
}
88+
89+
public byte[] decodeUsingDataTypeConverter(String hexString) {
90+
return DatatypeConverter.parseHexBinary(hexString);
91+
}
92+
93+
public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
94+
return Hex.encodeHexString(bytes);
95+
}
96+
97+
public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException {
98+
return Hex.decodeHex(hexString);
99+
}
100+
101+
public String encodeUsingGuava(byte[] bytes) {
102+
return BaseEncoding.base16()
103+
.encode(bytes);
104+
}
105+
106+
public byte[] decodeUsingGuava(String hexString) {
107+
return BaseEncoding.base16()
108+
.decode(hexString.toUpperCase());
109+
}
110+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.baeldung.algorithms.heapsort;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Heap<E extends Comparable<E>> {
8+
9+
private List<E> elements = new ArrayList<>();
10+
11+
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) {
12+
Heap<E> heap = of(elements);
13+
14+
List<E> result = new ArrayList<>();
15+
16+
while (!heap.isEmpty()) {
17+
result.add(heap.pop());
18+
}
19+
20+
return result;
21+
}
22+
23+
public static <E extends Comparable<E>> Heap<E> of(E... elements) {
24+
return of(Arrays.asList(elements));
25+
}
26+
27+
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) {
28+
Heap<E> result = new Heap<>();
29+
for (E element : elements) {
30+
result.add(element);
31+
}
32+
return result;
33+
}
34+
35+
public void add(E e) {
36+
elements.add(e);
37+
int elementIndex = elements.size() - 1;
38+
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) {
39+
int parentIndex = parentIndex(elementIndex);
40+
swap(elementIndex, parentIndex);
41+
elementIndex = parentIndex;
42+
}
43+
}
44+
45+
public E pop() {
46+
if (isEmpty()) {
47+
throw new IllegalStateException("You cannot pop from an empty heap");
48+
}
49+
50+
E result = elementAt(0);
51+
52+
int lasElementIndex = elements.size() - 1;
53+
swap(0, lasElementIndex);
54+
elements.remove(lasElementIndex);
55+
56+
int elementIndex = 0;
57+
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) {
58+
int smallerChildIndex = smallerChildIndex(elementIndex);
59+
swap(elementIndex, smallerChildIndex);
60+
elementIndex = smallerChildIndex;
61+
}
62+
63+
return result;
64+
}
65+
66+
public boolean isEmpty() {
67+
return elements.isEmpty();
68+
}
69+
70+
private boolean isRoot(int index) {
71+
return index == 0;
72+
}
73+
74+
private int smallerChildIndex(int index) {
75+
int leftChildIndex = leftChildIndex(index);
76+
int rightChildIndex = rightChildIndex(index);
77+
78+
if (!isValidIndex(rightChildIndex)) {
79+
return leftChildIndex;
80+
}
81+
82+
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) {
83+
return leftChildIndex;
84+
}
85+
86+
return rightChildIndex;
87+
}
88+
89+
private boolean isLeaf(int index) {
90+
return !isValidIndex(leftChildIndex(index));
91+
}
92+
93+
private boolean isCorrectParent(int index) {
94+
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index));
95+
}
96+
97+
private boolean isCorrectChild(int index) {
98+
return isCorrect(parentIndex(index), index);
99+
}
100+
101+
private boolean isCorrect(int parentIndex, int childIndex) {
102+
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) {
103+
return true;
104+
}
105+
106+
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0;
107+
}
108+
109+
private boolean isValidIndex(int index) {
110+
return index < elements.size();
111+
}
112+
113+
private void swap(int index1, int index2) {
114+
E element1 = elementAt(index1);
115+
E element2 = elementAt(index2);
116+
elements.set(index1, element2);
117+
elements.set(index2, element1);
118+
}
119+
120+
private E elementAt(int index) {
121+
return elements.get(index);
122+
}
123+
124+
private int parentIndex(int index) {
125+
return (index - 1) / 2;
126+
}
127+
128+
private int leftChildIndex(int index) {
129+
return 2 * index + 1;
130+
}
131+
132+
private int rightChildIndex(int index) {
133+
return 2 * index + 2;
134+
}
135+
136+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.algorithms.insertionsort;
2+
3+
public class InsertionSort {
4+
5+
public static void insertionSortImperative(int[] input) {
6+
for (int i = 1; i < input.length; i++) {
7+
int key = input[i];
8+
int j = i - 1;
9+
while (j >= 0 && input[j] > key) {
10+
input[j + 1] = input[j];
11+
j = j - 1;
12+
}
13+
input[j + 1] = key;
14+
}
15+
}
16+
17+
public static void insertionSortRecursive(int[] input) {
18+
insertionSortRecursive(input, input.length);
19+
}
20+
21+
private static void insertionSortRecursive(int[] input, int i) {
22+
// base case
23+
if (i <= 1) {
24+
return;
25+
}
26+
27+
// sort the first i - 1 elements of the array
28+
insertionSortRecursive(input, i - 1);
29+
30+
// then find the correct position of the element at position i
31+
int key = input[i - 1];
32+
int j = i - 2;
33+
// shifting the elements from their position by 1
34+
while (j >= 0 && input[j] > key) {
35+
input[j + 1] = input[j];
36+
j = j - 1;
37+
}
38+
// inserting the key at the appropriate position
39+
input[j + 1] = key;
40+
}
41+
}

0 commit comments

Comments
 (0)