Skip to content

Commit

Permalink
Merge remote-tracking branch 'central/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzer committed Sep 25, 2017
2 parents 23018fa + 2beb5f0 commit 2db8ba4
Show file tree
Hide file tree
Showing 1,001 changed files with 25,907 additions and 3,138 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties

spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
*.springBeans

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons

CI - Jenkins
================================
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials/)**
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/github%20projects%20Jobs/job/tutorials/)**
1 change: 1 addition & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
26 changes: 0 additions & 26 deletions algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.algorithms.binarysearch;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearch {

public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {

int index = Integer.MAX_VALUE;

while (low <= high) {

int mid = (low + high) / 2;

if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}

public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {

int middle = (low + high) / 2;
if (high < low) {
return -1;
}

if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
}
}

public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}

public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionBruteForce {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Node<T> it1 = head;
int nodesTraversedByOuter = 0;
while (it1 != null && it1.next != null) {
it1 = it1.next;
nodesTraversedByOuter++;

int x = nodesTraversedByOuter;
Node<T> it2 = head;
int noOfTimesCurrentNodeVisited = 0;

while (x > 0) {
it2 = it2.next;

if (it2 == it1) {
noOfTimesCurrentNodeVisited++;
}

if (noOfTimesCurrentNodeVisited == 2) {
return new CycleDetectionResult<>(true, it1);
}

x--;
}
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionByFastAndSlowIterators {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Node<T> slow = head;
Node<T> fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

if (slow == fast) {
return new CycleDetectionResult<>(true, fast);
}
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.algorithms.linkedlist;

import java.util.HashSet;
import java.util.Set;

public class CycleDetectionByHashing {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Set<Node<T>> set = new HashSet<>();
Node<T> node = head;

while (node != null) {
if (set.contains(node)) {
return new CycleDetectionResult<>(true, node);
}
set.add(node);
node = node.next;
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionResult<T> {
boolean cycleExists;
Node<T> node;

public CycleDetectionResult(boolean cycleExists, Node<T> node) {
super();
this.cycleExists = cycleExists;
this.node = node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalBruteForce {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

/**
* @param loopNodeParam - reference to the node where Flyods cycle
* finding algorithm ends, i.e. the fast and the slow iterators
* meet.
* @param head - reference to the head of the list
*/
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
Node<T> it = head;

while (it != null) {
if (isNodeReachableFromLoopNode(it, loopNodeParam)) {
Node<T> loopStart = it;
findEndNodeAndBreakCycle(loopStart);
break;
}
it = it.next;
}
}

private static <T> boolean isNodeReachableFromLoopNode(Node<T> it, Node<T> loopNodeParam) {
Node<T> loopNode = loopNodeParam;

do {
if (it == loopNode) {
return true;
}
loopNode = loopNode.next;
} while (loopNode.next != loopNodeParam);

return false;
}

private static <T> void findEndNodeAndBreakCycle(Node<T> loopStartParam) {
Node<T> loopStart = loopStartParam;

while (loopStart.next != loopStartParam) {
loopStart = loopStart.next;
}

loopStart.next = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalByCountingLoopNodes {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
int cycleLength = calculateCycleLength(loopNodeParam);
Node<T> cycleLengthAdvancedIterator = head;
Node<T> it = head;

for (int i = 0; i < cycleLength; i++) {
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
}

while (it.next != cycleLengthAdvancedIterator.next) {
it = it.next;
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
}

cycleLengthAdvancedIterator.next = null;
}

private static <T> int calculateCycleLength(Node<T> loopNodeParam) {
Node<T> loopNode = loopNodeParam;
int length = 1;

while (loopNode.next != loopNodeParam) {
length++;
loopNode = loopNode.next;
}

return length;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalWithoutCountingLoopNodes {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

private static <T> void removeCycle(Node<T> meetingPointParam, Node<T> head) {
Node<T> loopNode = meetingPointParam;
Node<T> it = head;

while (loopNode.next != it.next) {
it = it.next;
loopNode = loopNode.next;
}

loopNode.next = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.algorithms.linkedlist;

public class Node<T> {
T data;
Node<T> next;

public static <T> Node<T> createNewNode(T data, Node<T> next) {
Node<T> node = new Node<T>();
node.data = data;
node.next = next;
return node;
}

public static <T> void traverseList(Node<T> root) {
if (root == null) {
return;
}

Node<T> node = root;
while (node != null) {
System.out.println(node.data);
node = node.next;
}
}

public static <T> Node<T> getTail(Node<T> root) {
if (root == null) {
return null;
}

Node<T> node = root;
while (node.next != null) {
node = node.next;
}
return node;
}

}
Loading

0 comments on commit 2db8ba4

Please sign in to comment.