forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'central/master'
- Loading branch information
Showing
1,001 changed files
with
25,907 additions
and
3,138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
algorithms/src/main/java/com/baeldung/algorithms/binarysearch/BinarySearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...rc/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...hms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...rc/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
algorithms/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.