Merge branch 'master' of https://github.com/vikasrajput6035/tutorials into BAEL-3504
This commit is contained in:
commit
6f4ebbac9f
|
@ -0,0 +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;
|
||||
}
|
||||
}
|
|
@ -0,0 +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;
|
||||
}
|
||||
}
|
|
@ -0,0 +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)));
|
||||
}
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ import org.junit.Test;
|
|||
public class TernaryOperatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenACondition_whenUsingTernaryOperator_thenItEvaluatesConditionAndReturnsAValue() {
|
||||
public void whenUsingTernaryOperator_thenConditionIsEvaluatedAndValueReturned() {
|
||||
int number = 10;
|
||||
String msg = number > 10 ? "Number is greater than 10" : "Number is less than or equal to 10";
|
||||
|
||||
|
@ -14,7 +14,7 @@ public class TernaryOperatorUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenATrueCondition_whenUsingTernaryOperator_thenOnlyExpression1IsEvaluated() {
|
||||
public void whenConditionIsTrue_thenOnlyFirstExpressionIsEvaluated() {
|
||||
int exp1 = 0, exp2 = 0;
|
||||
int result = 12 > 10 ? ++exp1 : ++exp2;
|
||||
|
||||
|
@ -24,7 +24,7 @@ public class TernaryOperatorUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenAFalseCondition_whenUsingTernaryOperator_thenOnlyExpression2IsEvaluated() {
|
||||
public void whenConditionIsFalse_thenOnlySecondExpressionIsEvaluated() {
|
||||
int exp1 = 0, exp2 = 0;
|
||||
int result = 8 > 10 ? ++exp1 : ++exp2;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.themes.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
|
@ -27,7 +27,7 @@ public class DataSourceConfig {
|
|||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
|
||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource);
|
||||
em.setPackagesToScan("com.baeldung.domain");
|
||||
em.setPackagesToScan("com.baeldung.themes.domain");
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
return em;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.themes.config;
|
||||
|
||||
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.themes.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.themes.config;
|
||||
|
||||
import com.baeldung.theme.resolver.UserPreferenceThemeResolver;
|
||||
import com.baeldung.themes.resolver.UserPreferenceThemeResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.themes.config;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.controllers;
|
||||
package com.baeldung.themes.controllers;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.themes.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.repository;
|
||||
package com.baeldung.themes.repository;
|
||||
|
||||
import com.baeldung.domain.UserPreference;
|
||||
import com.baeldung.themes.domain.UserPreference;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
public interface UserPreferenceRepository extends PagingAndSortingRepository<UserPreference, String> {
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.theme.resolver;
|
||||
package com.baeldung.themes.resolver;
|
||||
|
||||
import com.baeldung.domain.UserPreference;
|
||||
import com.baeldung.repository.UserPreferenceRepository;
|
||||
import com.baeldung.themes.domain.UserPreference;
|
||||
import com.baeldung.themes.repository.UserPreferenceRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
@ -5,7 +5,7 @@
|
|||
<artifactId>spring-scheduling</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-scheduling</name>
|
||||
<packaging>war</packaging>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
Loading…
Reference in New Issue