diff --git a/algorithms-miscellaneous-5/README.md b/algorithms-miscellaneous-5/README.md index 6e0de7d64e..3e6eeb4c93 100644 --- a/algorithms-miscellaneous-5/README.md +++ b/algorithms-miscellaneous-5/README.md @@ -15,9 +15,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g. - [Maximum Subarray Problem](https://www.baeldung.com/java-maximum-subarray) - [How to Merge Two Sorted Arrays](https://www.baeldung.com/java-merge-sorted-arrays) - [Median of Stream of Integers using Heap](https://www.baeldung.com/java-stream-integers-median-using-heap) -- [Kruskal’s Algorithm for Spanning Trees](https://www.baeldung.com/java-spanning-trees-kruskal) -- [Balanced Brackets Algorithm in Java](https://www.baeldung.com/java-balanced-brackets-algorithm) -- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences) -- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms) -- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher) -- More articles: [[<-- prev]](/../algorithms-miscellaneous-4) +- More articles: [[<-- prev]](/../algorithms-miscellaneous-4) [[next -->]](/../algorithms-miscellaneous-6) + diff --git a/algorithms-miscellaneous-5/pom.xml b/algorithms-miscellaneous-5/pom.xml index 4131e1791d..9d0326241a 100644 --- a/algorithms-miscellaneous-5/pom.xml +++ b/algorithms-miscellaneous-5/pom.xml @@ -21,16 +21,10 @@ ${commons-codec.version} - org.apache.commons - commons-math3 - ${commons-math3.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - + org.apache.commons + commons-math3 + ${commons-math3.version} + pl.allegro.finance tradukisto diff --git a/algorithms-miscellaneous-6/README.md b/algorithms-miscellaneous-6/README.md index 99be63d7ca..22ee51530f 100644 --- a/algorithms-miscellaneous-6/README.md +++ b/algorithms-miscellaneous-6/README.md @@ -2,3 +2,9 @@ - [Boruvka’s Algorithm for Minimum Spanning Trees](https://www.baeldung.com/java-boruvka-algorithm) - [Gradient Descent in Java](https://www.baeldung.com/java-gradient-descent) +- [Kruskal’s Algorithm for Spanning Trees](https://www.baeldung.com/java-spanning-trees-kruskal) +- [Balanced Brackets Algorithm in Java](https://www.baeldung.com/java-balanced-brackets-algorithm) +- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences) +- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms) +- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher) +- More articles: [[<-- prev]](/../algorithms-miscellaneous-5) diff --git a/algorithms-miscellaneous-6/pom.xml b/algorithms-miscellaneous-6/pom.xml index fda9cf10f9..0a0edcbb3f 100644 --- a/algorithms-miscellaneous-6/pom.xml +++ b/algorithms-miscellaneous-6/pom.xml @@ -18,10 +18,35 @@ guava ${guava.version} + + org.junit.platform + junit-platform-commons + ${junit.platform.version} + + + org.assertj + assertj-core + ${org.assertj.core.version} + test + + + org.projectlombok + lombok + ${lombok.version} + provided + + + org.apache.commons + commons-math3 + ${commons-math3.version} + 28.1-jre + 3.9.0 + 1.6.0 + 3.6.1 diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingString.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingString.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingString.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingString.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/caesarcipher/CaesarCipher.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/caesarcipher/CaesarCipher.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/caesarcipher/CaesarCipher.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/caesarcipher/CaesarCipher.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/Follower.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/Follower.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/CycleDetector.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/CycleDetector.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/CycleDetector.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/CycleDetector.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/DisjointSetInfo.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/DisjointSetInfo.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/DisjointSetInfo.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/DisjointSetInfo.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/Kruskal.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/Kruskal.java similarity index 100% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/kruskal/Kruskal.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/kruskal/Kruskal.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java similarity index 95% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java index 9e8439cb87..e2e41baf4d 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java +++ b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/HeapNode.java @@ -1,13 +1,13 @@ -package com.baeldung.algorithms.minheapmerge; - -public class HeapNode { - - int element; - int arrayIndex; - int nextElementIndex = 1; - - public HeapNode(int element, int arrayIndex) { - this.element = element; - this.arrayIndex = arrayIndex; - } -} +package com.baeldung.algorithms.minheapmerge; + +public class HeapNode { + + int element; + int arrayIndex; + int nextElementIndex = 1; + + public HeapNode(int element, int arrayIndex) { + this.element = element; + this.arrayIndex = arrayIndex; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java similarity index 96% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java rename to algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java index b77ce43160..0217480e9e 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java +++ b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/minheapmerge/MinHeap.java @@ -1,88 +1,88 @@ -package com.baeldung.algorithms.minheapmerge; - -public class MinHeap { - - HeapNode[] heapNodes; - - public MinHeap(HeapNode heapNodes[]) { - this.heapNodes = heapNodes; - heapifyFromLastLeafsParent(); - } - - void heapifyFromLastLeafsParent() { - int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length); - while (lastLeafsParentIndex >= 0) { - heapify(lastLeafsParentIndex); - lastLeafsParentIndex--; - } - } - - void heapify(int index) { - int leftNodeIndex = getLeftNodeIndex(index); - int rightNodeIndex = getRightNodeIndex(index); - int smallestElementIndex = index; - if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) { - smallestElementIndex = leftNodeIndex; - } - if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) { - smallestElementIndex = rightNodeIndex; - } - if (smallestElementIndex != index) { - swap(index, smallestElementIndex); - heapify(smallestElementIndex); - } - } - - int getParentNodeIndex(int index) { - return (index - 1) / 2; - } - - int getLeftNodeIndex(int index) { - return (2 * index + 1); - } - - int getRightNodeIndex(int index) { - return (2 * index + 2); - } - - HeapNode getRootNode() { - return heapNodes[0]; - } - - void heapifyFromRoot() { - heapify(0); - } - - void swap(int i, int j) { - HeapNode temp = heapNodes[i]; - heapNodes[i] = heapNodes[j]; - heapNodes[j] = temp; - } - - static int[] merge(int[][] array) { - HeapNode[] heapNodes = new HeapNode[array.length]; - int resultingArraySize = 0; - - for (int i = 0; i < array.length; i++) { - HeapNode node = new HeapNode(array[i][0], i); - heapNodes[i] = node; - resultingArraySize += array[i].length; - } - - MinHeap minHeap = new MinHeap(heapNodes); - int[] resultingArray = new int[resultingArraySize]; - - for (int i = 0; i < resultingArraySize; i++) { - HeapNode root = minHeap.getRootNode(); - resultingArray[i] = root.element; - - if (root.nextElementIndex < array[root.arrayIndex].length) { - root.element = array[root.arrayIndex][root.nextElementIndex++]; - } else { - root.element = Integer.MAX_VALUE; - } - minHeap.heapifyFromRoot(); - } - return resultingArray; - } -} +package com.baeldung.algorithms.minheapmerge; + +public class MinHeap { + + HeapNode[] heapNodes; + + public MinHeap(HeapNode heapNodes[]) { + this.heapNodes = heapNodes; + heapifyFromLastLeafsParent(); + } + + void heapifyFromLastLeafsParent() { + int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length); + while (lastLeafsParentIndex >= 0) { + heapify(lastLeafsParentIndex); + lastLeafsParentIndex--; + } + } + + void heapify(int index) { + int leftNodeIndex = getLeftNodeIndex(index); + int rightNodeIndex = getRightNodeIndex(index); + int smallestElementIndex = index; + if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) { + smallestElementIndex = leftNodeIndex; + } + if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) { + smallestElementIndex = rightNodeIndex; + } + if (smallestElementIndex != index) { + swap(index, smallestElementIndex); + heapify(smallestElementIndex); + } + } + + int getParentNodeIndex(int index) { + return (index - 1) / 2; + } + + int getLeftNodeIndex(int index) { + return (2 * index + 1); + } + + int getRightNodeIndex(int index) { + return (2 * index + 2); + } + + HeapNode getRootNode() { + return heapNodes[0]; + } + + void heapifyFromRoot() { + heapify(0); + } + + void swap(int i, int j) { + HeapNode temp = heapNodes[i]; + heapNodes[i] = heapNodes[j]; + heapNodes[j] = temp; + } + + static int[] merge(int[][] array) { + HeapNode[] heapNodes = new HeapNode[array.length]; + int resultingArraySize = 0; + + for (int i = 0; i < array.length; i++) { + HeapNode node = new HeapNode(array[i][0], i); + heapNodes[i] = node; + resultingArraySize += array[i].length; + } + + MinHeap minHeap = new MinHeap(heapNodes); + int[] resultingArray = new int[resultingArraySize]; + + for (int i = 0; i < resultingArraySize; i++) { + HeapNode root = minHeap.getRootNode(); + resultingArray[i] = root.element; + + if (root.nextElementIndex < array[root.arrayIndex].length) { + root.element = array[root.arrayIndex][root.nextElementIndex++]; + } else { + root.element = Integer.MAX_VALUE; + } + minHeap.heapifyFromRoot(); + } + return resultingArray; + } +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java similarity index 100% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStringUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStringUnitTest.java similarity index 100% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStringUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStringUnitTest.java diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/caesarcipher/CaesarCipherUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/caesarcipher/CaesarCipherUnitTest.java similarity index 100% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/caesarcipher/CaesarCipherUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/caesarcipher/CaesarCipherUnitTest.java diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java similarity index 100% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/kruskal/KruskalUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/kruskal/KruskalUnitTest.java similarity index 100% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/kruskal/KruskalUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/kruskal/KruskalUnitTest.java diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java similarity index 96% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java rename to algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java index 80d0d20f05..f84c860dcc 100644 --- a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java +++ b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/minheapmerge/MinHeapUnitTest.java @@ -1,22 +1,22 @@ -package com.baeldung.algorithms.minheapmerge; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import org.junit.Test; - -public class MinHeapUnitTest { - - private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } }; - private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 }; - - @Test - public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() { - int[] resultArray = MinHeap.merge(inputArray); - - assertThat(resultArray.length, is(equalTo(10))); - assertThat(resultArray, is(equalTo(expectedArray))); - } - -} +package com.baeldung.algorithms.minheapmerge; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class MinHeapUnitTest { + + private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } }; + private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 }; + + @Test + public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() { + int[] resultArray = MinHeap.merge(inputArray); + + assertThat(resultArray.length, is(equalTo(10))); + assertThat(resultArray, is(equalTo(expectedArray))); + } + +}