Java-1520 Split or move algorithms-miscellaneous-5 module

This commit is contained in:
mikr 2020-04-30 18:11:37 +02:00
parent 56f7b49988
commit f19c4d9b5d
24 changed files with 160 additions and 139 deletions

View File

@ -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) - [Maximum Subarray Problem](https://www.baeldung.com/java-maximum-subarray)
- [How to Merge Two Sorted Arrays](https://www.baeldung.com/java-merge-sorted-arrays) - [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) - [Median of Stream of Integers using Heap](https://www.baeldung.com/java-stream-integers-median-using-heap)
- [Kruskals Algorithm for Spanning Trees](https://www.baeldung.com/java-spanning-trees-kruskal) - More articles: [[<-- prev]](/../algorithms-miscellaneous-4) [[next -->]](/../algorithms-miscellaneous-6)
- [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)

View File

@ -21,16 +21,10 @@
<version>${commons-codec.version}</version> <version>${commons-codec.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId> <artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version> <version>${commons-math3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>pl.allegro.finance</groupId> <groupId>pl.allegro.finance</groupId>
<artifactId>tradukisto</artifactId> <artifactId>tradukisto</artifactId>

View File

@ -2,3 +2,9 @@
- [Boruvkas Algorithm for Minimum Spanning Trees](https://www.baeldung.com/java-boruvka-algorithm) - [Boruvkas Algorithm for Minimum Spanning Trees](https://www.baeldung.com/java-boruvka-algorithm)
- [Gradient Descent in Java](https://www.baeldung.com/java-gradient-descent) - [Gradient Descent in Java](https://www.baeldung.com/java-gradient-descent)
- [Kruskals 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)

View File

@ -18,10 +18,35 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${org.assertj.core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<guava.version>28.1-jre</guava.version> <guava.version>28.1-jre</guava.version>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<junit.platform.version>1.6.0</junit.platform.version>
<commons-math3.version>3.6.1</commons-math3.version>
</properties> </properties>
</project> </project>

View File

@ -1,13 +1,13 @@
package com.baeldung.algorithms.minheapmerge; package com.baeldung.algorithms.minheapmerge;
public class HeapNode { public class HeapNode {
int element; int element;
int arrayIndex; int arrayIndex;
int nextElementIndex = 1; int nextElementIndex = 1;
public HeapNode(int element, int arrayIndex) { public HeapNode(int element, int arrayIndex) {
this.element = element; this.element = element;
this.arrayIndex = arrayIndex; this.arrayIndex = arrayIndex;
} }
} }

View File

@ -1,88 +1,88 @@
package com.baeldung.algorithms.minheapmerge; package com.baeldung.algorithms.minheapmerge;
public class MinHeap { public class MinHeap {
HeapNode[] heapNodes; HeapNode[] heapNodes;
public MinHeap(HeapNode heapNodes[]) { public MinHeap(HeapNode heapNodes[]) {
this.heapNodes = heapNodes; this.heapNodes = heapNodes;
heapifyFromLastLeafsParent(); heapifyFromLastLeafsParent();
} }
void heapifyFromLastLeafsParent() { void heapifyFromLastLeafsParent() {
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length); int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
while (lastLeafsParentIndex >= 0) { while (lastLeafsParentIndex >= 0) {
heapify(lastLeafsParentIndex); heapify(lastLeafsParentIndex);
lastLeafsParentIndex--; lastLeafsParentIndex--;
} }
} }
void heapify(int index) { void heapify(int index) {
int leftNodeIndex = getLeftNodeIndex(index); int leftNodeIndex = getLeftNodeIndex(index);
int rightNodeIndex = getRightNodeIndex(index); int rightNodeIndex = getRightNodeIndex(index);
int smallestElementIndex = index; int smallestElementIndex = index;
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) { if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) {
smallestElementIndex = leftNodeIndex; smallestElementIndex = leftNodeIndex;
} }
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) { if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) {
smallestElementIndex = rightNodeIndex; smallestElementIndex = rightNodeIndex;
} }
if (smallestElementIndex != index) { if (smallestElementIndex != index) {
swap(index, smallestElementIndex); swap(index, smallestElementIndex);
heapify(smallestElementIndex); heapify(smallestElementIndex);
} }
} }
int getParentNodeIndex(int index) { int getParentNodeIndex(int index) {
return (index - 1) / 2; return (index - 1) / 2;
} }
int getLeftNodeIndex(int index) { int getLeftNodeIndex(int index) {
return (2 * index + 1); return (2 * index + 1);
} }
int getRightNodeIndex(int index) { int getRightNodeIndex(int index) {
return (2 * index + 2); return (2 * index + 2);
} }
HeapNode getRootNode() { HeapNode getRootNode() {
return heapNodes[0]; return heapNodes[0];
} }
void heapifyFromRoot() { void heapifyFromRoot() {
heapify(0); heapify(0);
} }
void swap(int i, int j) { void swap(int i, int j) {
HeapNode temp = heapNodes[i]; HeapNode temp = heapNodes[i];
heapNodes[i] = heapNodes[j]; heapNodes[i] = heapNodes[j];
heapNodes[j] = temp; heapNodes[j] = temp;
} }
static int[] merge(int[][] array) { static int[] merge(int[][] array) {
HeapNode[] heapNodes = new HeapNode[array.length]; HeapNode[] heapNodes = new HeapNode[array.length];
int resultingArraySize = 0; int resultingArraySize = 0;
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
HeapNode node = new HeapNode(array[i][0], i); HeapNode node = new HeapNode(array[i][0], i);
heapNodes[i] = node; heapNodes[i] = node;
resultingArraySize += array[i].length; resultingArraySize += array[i].length;
} }
MinHeap minHeap = new MinHeap(heapNodes); MinHeap minHeap = new MinHeap(heapNodes);
int[] resultingArray = new int[resultingArraySize]; int[] resultingArray = new int[resultingArraySize];
for (int i = 0; i < resultingArraySize; i++) { for (int i = 0; i < resultingArraySize; i++) {
HeapNode root = minHeap.getRootNode(); HeapNode root = minHeap.getRootNode();
resultingArray[i] = root.element; resultingArray[i] = root.element;
if (root.nextElementIndex < array[root.arrayIndex].length) { if (root.nextElementIndex < array[root.arrayIndex].length) {
root.element = array[root.arrayIndex][root.nextElementIndex++]; root.element = array[root.arrayIndex][root.nextElementIndex++];
} else { } else {
root.element = Integer.MAX_VALUE; root.element = Integer.MAX_VALUE;
} }
minHeap.heapifyFromRoot(); minHeap.heapifyFromRoot();
} }
return resultingArray; return resultingArray;
} }
} }

View File

@ -1,22 +1,22 @@
package com.baeldung.algorithms.minheapmerge; package com.baeldung.algorithms.minheapmerge;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import org.junit.Test; import org.junit.Test;
public class MinHeapUnitTest { public class MinHeapUnitTest {
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } }; 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 }; private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
@Test @Test
public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() { public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
int[] resultArray = MinHeap.merge(inputArray); int[] resultArray = MinHeap.merge(inputArray);
assertThat(resultArray.length, is(equalTo(10))); assertThat(resultArray.length, is(equalTo(10)));
assertThat(resultArray, is(equalTo(expectedArray))); assertThat(resultArray, is(equalTo(expectedArray)));
} }
} }