diff --git a/.travis.yml b/.travis.yml index 58364da4..3a3703f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,16 +6,20 @@ before_script: - echo $JAVA_OPTS - echo $ANT_OPTS +dist: trusty + language: java jdk: + - oraclejdk9 - oraclejdk8 # - oraclejdk7 # - oraclejdk6 -# - openjdk8 +# - openjdk9 + - openjdk8 - openjdk7 -# - openjdk6 +# - openjdk6 env: - TEST_SUITE=run_tests diff --git a/README.md b/README.md index 459c4d35..f4d8bf3a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ Java : Algorithms and Data Structure ![alt tag](https://api.travis-ci.org/phishman3579/java-algorithms-implementation.svg?branch=master) ============================== -Algorithms and Data Structures implemented in Java +The algorithms and data structures are implemented in Java. -This is a collection of algorithms and data structures which I've implement over the years in my academic and professional life. The code isn't overly-optimized but is written to be correct and readable. The algorithms and data structures are well tested and, unless noted, are believe to be 100% correct. +This is a collection of algorithms and data structures I've implemented in my academic and professional life. The code isn't optimized but is written to be correct and readable. The algorithms and data structures are tested and, unless noted, believed to be correct. ## Created by Justin Wetherell @@ -45,7 +45,7 @@ This is a collection of algorithms and data structures which I've implement over * [Hash Map (associative array)](src/com/jwetherell/algorithms/data_structures/HashMap.java) * [Interval Tree](src/com/jwetherell/algorithms/data_structures/IntervalTree.java) * [Implicit Key Treap](src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java) -* [KD Tree (k-dimensional tree or k-d tree)](src/com/jwetherell/algorithms/data_structures/KDTree.java) +* [KD Tree (k-dimensional tree or k-d tree)](src/com/jwetherell/algorithms/data_structures/KdTree.java) * [List [backed by an array or a linked list]](src/com/jwetherell/algorithms/data_structures/List.java) * [LCP Array (Longest Common Prefix) [backed by a Suffix Array]](src/com/jwetherell/algorithms/data_structures/LCPArray.java) * [Matrix](src/com/jwetherell/algorithms/data_structures/Matrix.java) diff --git a/src/com/jwetherell/algorithms/data_structures/BTree.java b/src/com/jwetherell/algorithms/data_structures/BTree.java index d15d398c..d930076b 100644 --- a/src/com/jwetherell/algorithms/data_structures/BTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BTree.java @@ -109,7 +109,7 @@ public boolean add(T value) { /** * The node's key size is greater than maxKeySize, split down the middle. * - * @param node + * @param nodeToSplit * to split. */ private void split(Node nodeToSplit) { diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index 61f38a16..08855e28 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -289,8 +289,8 @@ public boolean validate() { /** * Validate the node for the heap invariants. * - * @param node - * to validate for. + * @param index + * of node to validate for. * @return True if node is valid. */ private boolean validateNode(int index) { @@ -341,7 +341,7 @@ public T[] getHeap() { */ @Override public T getHeadValue() { - if (array.length == 0) return null; + if (size == 0 || array.length == 0) return null; return array[0]; } @@ -372,7 +372,7 @@ public String toString() { protected static class HeapPrinter { public static > String getString(BinaryHeapArray tree) { - if (tree.array.length == 0) + if (tree.size == 0 || tree.array.length == 0) return "Tree has no nodes."; T root = tree.array[0]; @@ -449,8 +449,8 @@ public int size() { /** * Get the navigation directions through the tree to the index. * - * @param index - * of the Node to get directions for. + * @param idx + * index of the Node to get directions for. * @return Integer array representing the directions to the index. */ private static int[] getDirections(int idx) { diff --git a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java index d2b832a5..f956090c 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java @@ -185,7 +185,7 @@ protected void rotateLeft(Node node) { * Rotate tree right at sub-tree rooted at node. * * @param node - * Root of tree to rotate left. + * Root of tree to rotate right. */ protected void rotateRight(Node node) { Node parent = node.parent; diff --git a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java index bc64b598..b2796543 100644 --- a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java @@ -141,4 +141,9 @@ private Set getSuffixes(PatriciaTrie.Node node, String prefix) { public String toString() { return PatriciaTrie.PatriciaTriePrinter.getString(tree); } + + public boolean equals(CompactSuffixTrie trie){ + if(this.getSuffixes().equals(trie.getSuffixes())) return true; + return false; + } } diff --git a/src/com/jwetherell/algorithms/data_structures/HashMap.java b/src/com/jwetherell/algorithms/data_structures/HashMap.java index 76c669de..6556d4b9 100644 --- a/src/com/jwetherell/algorithms/data_structures/HashMap.java +++ b/src/com/jwetherell/algorithms/data_structures/HashMap.java @@ -212,9 +212,6 @@ private void initializeMap(int length) { * * @param h * hash to get index of. - * @param length - * length of array - * * @return Integer which represents the key. */ private int indexOf(int h) { diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java b/src/com/jwetherell/algorithms/data_structures/IntervalSum.java similarity index 77% rename from src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java rename to src/com/jwetherell/algorithms/data_structures/IntervalSum.java index 4f567ef5..b4c62f57 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalSum.java @@ -1,20 +1,24 @@ package com.jwetherell.algorithms.data_structures; +import java.util.List; +import java.util.ArrayList; /** * Implementation of array holding integer values which allows to count sum of elements in given * interval in O(log n) complexity. - * + *
* @author Szymon Stankiewicz + * @author Justin Wetherell */ -public class IntervalSumArray { - private List.ArrayList values = new List.ArrayList<>(); - private List.ArrayList prefSums = new List.ArrayList<>(); +public class IntervalSum { + + private List values = new ArrayList(); + private List prefSums = new ArrayList(); /** * Creates empty IntervalSumArray */ - public IntervalSumArray() { + public IntervalSum() { values.add(0); prefSums.add(0); } @@ -26,9 +30,8 @@ public IntervalSumArray() { * * @param size size of IntervalSumArray */ - public IntervalSumArray(int size) { - this(); - for(int i = 0; i values) { - this(); - for(Integer v: values) + public IntervalSum(Iterable values) { + for (Integer v: values) add(v); } private static int greatestPowerOfTwoDividing(int x) { - return x&(-x); + return x & (-x); } private static int successor(int x) { @@ -75,7 +77,7 @@ public int size() { */ public void add(int val) { values.add(val); - for(int i = 1; i= size()) throw new IndexOutOfBoundsException(); + if (index < 0 || index >= size()) + throw new IndexOutOfBoundsException(); index++; int diff = val - values.get(index); values.set(index, val); - while(index <= size()) { + while (index <= size()) { int oldPrefSum = prefSums.get(index); prefSums.set(index, oldPrefSum + diff); index = successor(index); @@ -121,10 +124,11 @@ public int get(int index) { * @return sum of values in interval */ public int sum(int end) { - if(end < 0 || end >= size()) throw new IndexOutOfBoundsException(); + if (end < 0 || end >= size()) + throw new IndexOutOfBoundsException(); end++; int s = 0; - while(end > 0) { + while (end > 0) { s += prefSums.get(end); end = predecessor(end); } @@ -152,7 +156,8 @@ public int sum() { * @return sum of values in interval */ public int sum(int start, int end) { - if(start > end) throw new IllegalArgumentException("Start must be less then end"); + if (start > end) + throw new IllegalArgumentException("Start must be less then end"); int startPrefSum = start == 0 ? 0 : sum(start-1); int endPrefSum = sum(end); return endPrefSum - startPrefSum; diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java index a04113ba..1a82ef30 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java @@ -314,7 +314,7 @@ public IntervalData(long start, long end, O object) { /** * Interval data list which should all be unique * - * @param list + * @param set * of interval data objects */ public IntervalData(long start, long end, Set set) { @@ -389,10 +389,9 @@ public IntervalData copy() { /** * Query inside this data object. * - * @param start - * of range to query for. - * @param end - * of range to query for. + * @param index + * to find Data. + * * @return Data queried for or NULL if it doesn't match the query. */ public IntervalData query(long index) { diff --git a/src/com/jwetherell/algorithms/data_structures/List.java b/src/com/jwetherell/algorithms/data_structures/List.java index 37eb1db8..09d03a8c 100644 --- a/src/com/jwetherell/algorithms/data_structures/List.java +++ b/src/com/jwetherell/algorithms/data_structures/List.java @@ -67,8 +67,8 @@ public boolean remove(T value) { for (int i = 0; i < size; i++) { T obj = array[i]; if (obj.equals(value)) { - if (remove(i)!=null) return true; - return false; + remove(i); + return true; } } return false; diff --git a/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java new file mode 100644 index 00000000..5d9f0bca --- /dev/null +++ b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java @@ -0,0 +1,175 @@ +package com.jwetherell.algorithms.data_structures; + +import java.util.ArrayList; +import java.util.List; + +/** + * Structure for storing rooted tree which allows to find lowest common ancestor. + *

+ * @param type of value stored in nodes. + *
+ * @author Szymon Stankiewicz + * @author Justin Wetherell + */ +public class LowestCommonAncestor { + + /** + * Exception which can be thrown by lowestCommonAncestor function if two + * nodes are in different trees. + * + */ + public static class NodesNotInSameTreeException extends Exception { + private static final long serialVersionUID = -5366787886097250564L; + } + + /** + * Finds lower common ancestor of two nodes. + * + * Complexity O(log n) where n is the height of the tree. + * + * @param node1 first node + * @param node2 second node + * @return lower common ancestor + * @throws NodesNotInSameTreeException if nodes don't have common root + */ + public static TreeNode lowestCommonAncestor(TreeNode node1, TreeNode node2) throws NodesNotInSameTreeException { + if (node1 == node2) + return node1; + else if (node1.depth < node2.depth) + return lowestCommonAncestor(node2, node1); + else if (node1.depth > node2.depth) { + int diff = node1.depth - node2.depth; + int jump = 0; + while (diff > 0) { + if (diff % 2 == 1) + node1 = node1.ancestors.get(jump); + jump++; + diff /= 2; + } + return lowestCommonAncestor(node1, node2); + } else { + try { + int step = 0; + while (1<<(step+1) <= node1.depth) + step++; + while (step >= 0) { + if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) { + node1 = node1.ancestors.get(step); + node2 = node2.ancestors.get(step); + } + step--; + } + return node1.ancestors.get(0); + } catch (Exception e) { + throw new NodesNotInSameTreeException(); + } + + } + } + + public static final class TreeNode { + + private final List> ancestors = new ArrayList>(); + private final List> children = new ArrayList>(); + + private T value = null; + private int depth = 0; + + /** + * Creates tree with root only. + * + */ + public TreeNode() { } + + /** + * Creates tree with root (storing value) only. + * + * @param value value to be stored in root + */ + public TreeNode(T value) { + this.value = value; + } + + private TreeNode(TreeNode parent) { + parent.children.add(this); + this.ancestors.add(parent); + this.depth = parent.depth + 1; + int dist = 0; + while (true) { + try { + this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist)); + dist++; + } catch (Exception e){ + break; + } + } + } + + public TreeNode setValue(T value) { + this.value = value; + return this; + } + + /** + * Creates new child for this node and returns it. + * + * Complexity O(log depth) + * + * @return added child + */ + public TreeNode addChild() { + return new TreeNode(this); + } + + /** + * Creates new child (storing value) for this node and returns it. + * + * Complexity O(log depth) + * + * @param value value to be stored in new child + * @return added child + */ + public TreeNode addChild(T value) { + return addChild().setValue(value); + } + + /** + * Returns value stored in node. + * + * @return node's value. + */ + public T getValue() { + return value; + } + + /** + * Finds subtree with given value in the root. + * + * @param value value to be find + * @return subtree with given value in the root + */ + public TreeNode find(T value) { + if (this.value == null) { + if (value == null) + return this; + } else if (this.value.equals(value)) + return this; + for (TreeNode child: children) { + final TreeNode res = child.find(value); + if (res != null) + return res; + } + return null; + } + + /** + * Returns true if tree contains a node with given value + * + * @param value to be checked + * @return true if tree contains node with given value, false otherwise + */ + public boolean contains(T value) { + return find(value) != null; + } + } +} diff --git a/src/com/jwetherell/algorithms/data_structures/Queue.java b/src/com/jwetherell/algorithms/data_structures/Queue.java index 1a078f71..92962a4d 100644 --- a/src/com/jwetherell/algorithms/data_structures/Queue.java +++ b/src/com/jwetherell/algorithms/data_structures/Queue.java @@ -111,17 +111,17 @@ private boolean remove(int index) { return true; } - // Grow the array by 50% and rearrange to make sequential + // Triple the size of the underlying array and rearrange to make sequential private void grow(int size) { int growSize = (size + (size<<1)); T[] temp = (T[]) new Object[growSize]; // Since the array can wrap around, make sure you grab the first chunk int adjLast = lastIndex % array.length; - if (adjLast < firstIndex) { - System.arraycopy(array, 0, temp, array.length-adjLast, adjLast+1); + if (adjLast > 0 && adjLast <= firstIndex) { + System.arraycopy(array, 0, temp, array.length-adjLast, adjLast); } // Copy the remaining - System.arraycopy(array, firstIndex, temp, 0, array.length-firstIndex); + System.arraycopy(array, firstIndex, temp, 0, array.length - firstIndex); array = null; array = temp; lastIndex = (lastIndex - firstIndex); @@ -134,9 +134,9 @@ private void shrink() { T[] temp = (T[]) new Object[shrinkSize]; // Since the array can wrap around, make sure you grab the first chunk int adjLast = lastIndex % array.length; - int endIndex = (lastIndex>array.length)?array.length:lastIndex; + int endIndex = (lastIndex>array.length) ? array.length : lastIndex; if (adjLast <= firstIndex) { - System.arraycopy(array, 0, temp, array.length-firstIndex, adjLast); + System.arraycopy(array, 0, temp, array.length - firstIndex, adjLast); } // Copy the remaining System.arraycopy(array, firstIndex, temp, 0, endIndex-firstIndex); @@ -563,7 +563,7 @@ public boolean hasNext() { public T next() { if (queue.firstIndex+index < queue.lastIndex) { last = queue.firstIndex+index; - return queue.array[queue.firstIndex+index++]; + return queue.array[(queue.firstIndex + index++) % queue.array.length]; } return null; } diff --git a/src/com/jwetherell/algorithms/data_structures/RootedTree.java b/src/com/jwetherell/algorithms/data_structures/RootedTree.java deleted file mode 100644 index 76ec95d2..00000000 --- a/src/com/jwetherell/algorithms/data_structures/RootedTree.java +++ /dev/null @@ -1,167 +0,0 @@ -package com.jwetherell.algorithms.data_structures; - -import java.util.ArrayList; - -/** - * Structure for storing rooted tree which allows to find lowest common ancestor. - * - * @param type of value stored in nodes. - */ -public class RootedTree { - private T value = null; - private int depth = 0; - private final ArrayList> ancestors = new ArrayList<>(); - private final ArrayList> children = new ArrayList<>(); - - /** - * Exception which can be thrown by lowestCommonAncestor function if two - * nodes are in different trees. - * - */ - public static class NodesNotInSameTreeException extends Exception {} - - /** - * Finds lower common ancestor of two nodes. - * - * Complexity O(log n) where n is the height of the tree. - * - * @param node1 first node - * @param node2 second node - * @return lower common ancestor - * @throws NodesNotInSameTreeException if nodes don't have common root - */ - public static RootedTree lowestCommonAncestor(RootedTree node1, RootedTree node2) throws NodesNotInSameTreeException { - if(node1 == node2) return node1; - else if(node1.depth < node2.depth) return lowestCommonAncestor(node2, node1); - else if(node1.depth > node2.depth) { - int diff = node1.depth - node2.depth; - int jump = 0; - while(diff > 0) { - if(diff % 2 == 1) - node1 = node1.ancestors.get(jump); - jump++; - diff /= 2; - } - return lowestCommonAncestor(node1, node2); - } - else { - try { - int step = 0; - while(1<<(step+1) <= node1.depth) step++; - while(step >= 0) { - if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) { - node1 = node1.ancestors.get(step); - node2 = node2.ancestors.get(step); - } - step--; - } - return node1.ancestors.get(0); - } catch (Exception e) { - throw new NodesNotInSameTreeException(); - } - - } - - } - - /** - * Creates tree with root only. - * - */ - public RootedTree() { - - } - - /** - * Cretes tree with root (storing value) only. - * - * @param value value to be stored in root - */ - public RootedTree(T value) { - this.value = value; - } - - private RootedTree(RootedTree parent) { - parent.children.add(this); - this.ancestors.add(parent); - this.depth = parent.depth + 1; - int dist = 0; - while(true) { - try { - this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist)); - dist++; - } catch (Exception e){ - break; - } - } - } - - public RootedTree setValue(T value) { - this.value = value; - return this; - } - - /** - * Creates new child for this node and returns it. - * - * Complexity O(log depth) - * - * @return added child - */ - public RootedTree addChild() { - return new RootedTree<>(this); - } - - /** - * Creates new child (storing value) for this node and returns it. - * - * Complexity O(log depth) - * - * @param value value to be stored in new child - * @return added child - */ - public RootedTree addChild(T value) { - return addChild().setValue(value); - } - - /** - * Returns value stored in node. - * - * @return node's value. - */ - public T getValue() { - return value; - } - - /** - * Finds subtree with given value in the root. - * - * @param value value to be find - * @return subtree with given value in the root - */ - public RootedTree find(T value) { - if(this.value == null) { - if(value == null) - return this; - } - else if(this.value.equals(value)) - return this; - for(RootedTree child: children) { - RootedTree res = child.find(value); - if(res != null) - return res; - } - return null; - } - - /** - * Returns true if tree contains a node with given value - * - * @param value to be checked - * @return true if tree contains node with given value, false otherwise - */ - public boolean contains(T value) { - return find(value) != null; - } - -} diff --git a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java index 9a04b8e6..fb516d8e 100644 --- a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java +++ b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java @@ -696,7 +696,7 @@ public IntervalData(long start, long end, O object) { /** * Interval data list which should all be unique * - * @param list + * @param set * of interval data objects */ public IntervalData(long start, long end, Set set) { diff --git a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java index 2d01ed9f..640754f0 100644 --- a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java +++ b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java @@ -165,6 +165,7 @@ private String buildStringWithEndChar(CharSequence sequence) { } private class KMRsWithIndex{ + Integer beginKMR; Integer endKMR; Integer index; diff --git a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java index dae44174..e096950c 100644 --- a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java +++ b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java @@ -25,7 +25,8 @@ private ConnectedComponents() { } /** * Finds the connected components subsets of the Graph. * - * @param g Graph to find connected components. + * @param graph + * to find connected components. * @return List of connected components in the Graph. */ public static final > List>> getConnectedComponents(Graph graph) { diff --git a/src/com/jwetherell/algorithms/mathematics/Knapsack.java b/src/com/jwetherell/algorithms/mathematics/Knapsack.java index 69f848c2..09079ce9 100644 --- a/src/com/jwetherell/algorithms/mathematics/Knapsack.java +++ b/src/com/jwetherell/algorithms/mathematics/Knapsack.java @@ -40,7 +40,7 @@ public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capac final List list = new ArrayList(); int i = height - 1; int j = width - 1; - while (i != 0 || j != 0) { + while (i != 0 && j != 0) { int current = output[i][j]; int above = output[i - 1][j]; if (current == above) { diff --git a/src/com/jwetherell/algorithms/numbers/Integers.java b/src/com/jwetherell/algorithms/numbers/Integers.java index 809ac8a6..fb86fd3d 100644 --- a/src/com/jwetherell/algorithms/numbers/Integers.java +++ b/src/com/jwetherell/algorithms/numbers/Integers.java @@ -119,7 +119,7 @@ public static final boolean powerOfTwoUsingBits(int numberToCheck) { singleDigits.put(14,"fourteen"); singleDigits.put(15,"fifteen"); singleDigits.put(16,"sixteen"); - singleDigits.put(17,"seventee"); + singleDigits.put(17,"seventeen"); singleDigits.put(18,"eighteen"); singleDigits.put(19,"nineteen"); } @@ -129,12 +129,12 @@ public static final boolean powerOfTwoUsingBits(int numberToCheck) { multiDigits.put(10,"ten"); multiDigits.put(20,"twenty"); multiDigits.put(30,"thirty"); - multiDigits.put(40,"fourty"); + multiDigits.put(40,"forty"); multiDigits.put(50,"fifty"); multiDigits.put(60,"sixty"); multiDigits.put(70,"seventy"); multiDigits.put(80,"eighty"); - multiDigits.put(90,"ninty"); + multiDigits.put(90,"ninety"); } private static final int BILLION = 1000000000; diff --git a/src/com/jwetherell/algorithms/search/BinarySearch.java b/src/com/jwetherell/algorithms/search/BinarySearch.java index 8d3f8521..3bcd557b 100644 --- a/src/com/jwetherell/algorithms/search/BinarySearch.java +++ b/src/com/jwetherell/algorithms/search/BinarySearch.java @@ -29,7 +29,8 @@ public static final int find(int value, int[] array, boolean optimize) { BinarySearch.sorted = null; } } - + //Recursively find the element + //@return find the element value by recursively private static int recursiveFind(int value, int start, int end, boolean optimize) { if (start == end) { int lastValue = sorted[start]; // start==end @@ -43,8 +44,10 @@ private static int recursiveFind(int value, int start, int end, boolean optimize final int middle = low + ((high - low) / 2); final int middleValue = sorted[middle]; + //checks if the middle index is element if (value == middleValue) return middle; + //if value is greater than move to right if (value > middleValue) { if (optimize && (end - middle) <= SWITCH_TO_BRUTE_FORCE) return linearSearch(value, middle + 1, end); @@ -54,8 +57,12 @@ private static int recursiveFind(int value, int start, int end, boolean optimize return linearSearch(value, start, middle - 1); return recursiveFind(value, start, middle - 1, optimize); } - + //Linear search to find the element. + //@value the element we want to find. + //@start first index of the array in the array + //@end last index of the array in the array. private static final int linearSearch(int value, int start, int end) { + // From index i = start to i = end check if value matches sorted[i] for (int i = start; i <= end; i++) { int iValue = sorted[i]; if (value == iValue) diff --git a/src/com/jwetherell/algorithms/search/LowerBound.java b/src/com/jwetherell/algorithms/search/LowerBound.java index 569e4532..9e6cd353 100644 --- a/src/com/jwetherell/algorithms/search/LowerBound.java +++ b/src/com/jwetherell/algorithms/search/LowerBound.java @@ -23,6 +23,7 @@ public static int lowerBound(int[] array, int length, int value) { int high = length; while (low < high) { final int mid = (low + high) / 2; + //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { diff --git a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java index 4421896b..598f6885 100644 --- a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java +++ b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java @@ -1,9 +1,5 @@ package com.jwetherell.algorithms.sequence; -import com.jwetherell.algorithms.search.LowerBound; - -import java.util.Arrays; - /** * In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in * which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. @@ -20,19 +16,43 @@ private LongestIncreasingSubsequence() { } /** * Longest increasing subsequence solved using dynamic programming. */ - public static int[] getLongestIncreasingSubsequence(int[] sequence) { - final int[] resultSequence = new int[sequence.length]; + public static int[] getLongestIncreasingSubsequence(int[] X) { + final int[] P = new int[X.length]; + final int[] M = new int[X.length+1]; + int L = 0; + for (int i=0; i L) { + // If we found a subsequence longer than any we've found yet, update L + L = newL; + } + } - int resultLength = 0; - for (int i = 0; i < sequence.length; ++i) { - // try to find the best place for new element in sorted result - final int pos = LowerBound.lowerBound(resultSequence, resultLength, sequence[i]); //O(log n) - // if the best place is at the end increase result length - if (pos >= resultLength) - resultLength++; - resultSequence[pos] = sequence[i]; + // Reconstruct the longest increasing subsequence + final int[] S = new int[L]; + int k = M[L]; + for (int i=L-1; i>=0; i--) { + S[i] = X[k]; + k = P[k]; } - return Arrays.copyOfRange(resultSequence, 0, resultLength); + return S; } } diff --git a/src/com/jwetherell/algorithms/sorts/BubbleSort.java b/src/com/jwetherell/algorithms/sorts/BubbleSort.java index 3ac320ff..824e4e17 100644 --- a/src/com/jwetherell/algorithms/sorts/BubbleSort.java +++ b/src/com/jwetherell/algorithms/sorts/BubbleSort.java @@ -21,7 +21,8 @@ public class BubbleSort> { private BubbleSort() { } - + //@param unsorted array + //@return sorted array public static > T[] sort(T[] unsorted) { boolean swapped = true; int length = unsorted.length; @@ -37,7 +38,7 @@ public static > T[] sort(T[] unsorted) { } return unsorted; } - + //swapping the value private static > void swap(int index1, int index2, T[] unsorted) { T value = unsorted[index1]; unsorted[index1] = unsorted[index2]; diff --git a/src/com/jwetherell/algorithms/sorts/CountingSort.java b/src/com/jwetherell/algorithms/sorts/CountingSort.java index f1593c50..31e6a289 100644 --- a/src/com/jwetherell/algorithms/sorts/CountingSort.java +++ b/src/com/jwetherell/algorithms/sorts/CountingSort.java @@ -27,26 +27,26 @@ private CountingSort() { } public static Integer[] sort(Integer[] unsorted) { int maxValue = findMax(unsorted); - int[] counts = new int[maxValue + 1]; + int[] counts = new int[maxValue + 1];//counts number of elements updateCounts(unsorted, counts); populateCounts(unsorted, counts); return unsorted; } - + //finding maximum value in unsorted array private static int findMax(Integer[] unsorted) { - int max = Integer.MIN_VALUE; + int max = Integer.MIN_VALUE;//assume minimum value(-2147483648) of interger is maximum for (int i : unsorted) { if (i > max) max = i; } return max; } - + //Incrementing the number of counts in unsorted array private static void updateCounts(Integer[] unsorted, int[] counts) { for (int e : unsorted) counts[e]++; } - + private static void populateCounts(Integer[] unsorted, int[] counts) { int index = 0; for (int i = 0; i < counts.length; i++) { diff --git a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java b/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java deleted file mode 100644 index bfed080e..00000000 --- a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.jwetherell.algorithms.data_structures; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class RootedTreeTest { - - @Test - public void largeTreeTest() throws RootedTree.NodesNotInSameTreeException { - RootedTree root = new RootedTree<>(); - RootedTree left = root.addChild(); - RootedTree middle = root.addChild(); - RootedTree right = root.addChild(); - - //long path - RootedTree v = left; - for(int i = 0; i<1000; i++) - v = v.addChild(); - RootedTree leftRight = left.addChild(); - - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); - - for(int i = 0; i<2000; i++) { - leftRight = leftRight.addChild(); - - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); - } - - assertEquals(RootedTree.lowestCommonAncestor(middle, right), root); - assertEquals(RootedTree.lowestCommonAncestor(root, right), root); - assertEquals(RootedTree.lowestCommonAncestor(root, root), root); - - RootedTree root2 = new RootedTree<>(); - boolean thrownException = false; - try { - RootedTree.lowestCommonAncestor(v, root2); - } catch (RootedTree.NodesNotInSameTreeException e) { - thrownException = true; - } - assertTrue(thrownException); - - RootedTree deepChild = v.addChild(101); - assertEquals(deepChild, root.find(101)); - assertTrue(root.contains(101)); - - assertNull(root.find(102)); - assertFalse(root.contains(102)); - } -} \ No newline at end of file diff --git a/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java b/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java index 865c266b..68591215 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java @@ -1,6 +1,7 @@ package com.jwetherell.algorithms.data_structures.test; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; import java.util.Collection; @@ -16,7 +17,7 @@ public class BinaryHeapTests { @Test public void testMinHeap() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(2500); String aNameMin = "Min-Heap [array]"; BinaryHeap.BinaryHeapArray aHeapMin = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); @@ -26,6 +27,12 @@ public void testMinHeap() { assertTrue(JavaCollectionTest.testCollection(aCollectionMin, Integer.class, aNameMin, data.unsorted, data.sorted, data.invalid)); + BinaryHeap.BinaryHeapArray aHeapNull = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); + aHeapNull.add(10); + aHeapNull.add(11); + aHeapNull.clear(); + assertNull(aHeapNull.getHeadValue()); // we expect null here + String tNameMin = "Min-Heap [tree]"; BinaryHeap.BinaryHeapTree tHeapMin = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); Collection tCollectionMin = tHeapMin.toCollection(); @@ -33,11 +40,18 @@ public void testMinHeap() { data.unsorted, data.sorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(tCollectionMin, Integer.class, tNameMin, data.unsorted, data.sorted, data.invalid)); + + BinaryHeap.BinaryHeapTree tHeapNull = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); + tHeapNull.add(10); + tHeapNull.add(11); + tHeapNull.clear(); + assertNull(tHeapNull.getHeadValue()); // we expect null here + } @Test public void testMaxHeap() { - TestData data = Utils.generateTestData(1000); + TestData data = Utils.generateTestData(2500); String aNameMax = "Max-Heap [array]"; BinaryHeap.BinaryHeapArray aHeapMax = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); @@ -47,6 +61,12 @@ public void testMaxHeap() { assertTrue(JavaCollectionTest.testCollection(aCollectionMax, Integer.class, aNameMax, data.unsorted, data.sorted, data.invalid)); + BinaryHeap.BinaryHeapArray aHeapNull = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); + aHeapNull.add(10); + aHeapNull.add(11); + aHeapNull.clear(); + assertNull(aHeapNull.getHeadValue()); // we expect null here + String lNameMax = "Max-Heap [tree]"; BinaryHeap.BinaryHeapTree tHeapMax = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); Collection tCollectionMax = tHeapMax.toCollection(); @@ -54,5 +74,11 @@ public void testMaxHeap() { data.unsorted, data.sorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(tCollectionMax, Integer.class, lNameMax, data.unsorted, data.sorted, data.invalid)); + + BinaryHeap.BinaryHeapTree tHeapNull = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); + tHeapNull.add(10); + tHeapNull.add(11); + tHeapNull.clear(); + assertNull(tHeapNull.getHeadValue()); // we expect null here } } diff --git a/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java b/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java index 8b6bce27..f32f3f64 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java @@ -25,4 +25,26 @@ public void testCompactSuffixTrie() { exists = trie.doesSubStringExist(pass); assertTrue("YIKES!! " + pass + " doesn't exists.", exists); } + + @Test + public void testCompactSuffixTrie_equals() { + String bookkeeper = "bookkeeper"; + CompactSuffixTrie trie = new CompactSuffixTrie(bookkeeper); + + String bookkeeper_1 = "bookkeeper"; + CompactSuffixTrie trie_1 = new CompactSuffixTrie(bookkeeper_1); + + boolean equal = trie.equals(trie_1); + assertTrue("YIKES!! " + bookkeeper + " and " + bookkeeper_1 + " are not equal.", equal); + + + String failed = "failed"; + trie = new CompactSuffixTrie(failed); + + String failed_1 = "failet"; + trie_1 = new CompactSuffixTrie(failed_1); + + equal = trie.equals(trie_1); + assertFalse("YIKES!! " + failed + " and " + failed_1 + " are equal.", equal); + } } diff --git a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java similarity index 57% rename from test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java rename to test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java index 0af23a19..2203ef02 100644 --- a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java @@ -1,50 +1,53 @@ -package com.jwetherell.algorithms.data_structures; +package com.jwetherell.algorithms.data_structures.test; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.Random; -import static org.junit.Assert.*; +import org.junit.Test; + +import com.jwetherell.algorithms.data_structures.IntervalSum; -public class IntervalSumArrayTest { +public class IntervalSumTest { @Test public void properSumAllElementsTest() { - IntervalSumArray sub = new IntervalSumArray(); - for(int i = 0; i<=100; i++) + final IntervalSum sub = new IntervalSum(); + for (int i = 0; i<=100; i++) sub.add(i); - for(int i = 0; i<=100; i++) + for (int i = 0; i<=100; i++) assertEquals(i*(i+1)/2, sub.sum(i)); assertEquals(100*101/2, sub.sum()); } @Test public void randomGeneratedTest() { - Random generator = new Random(42); - List list = new ArrayList<>(); - for(int i = 0; i<=100; i++) + final Random generator = new Random(42); + final List list = new ArrayList(); + for (int i = 0; i<=100; i++) list.add(i); - IntervalSumArray sum = new IntervalSumArray(list); - for(int i = 0; i<1000000; i++) { - int pos = generator.nextInt(100); - int val = generator.nextInt(2000000) - 1000000; + final IntervalSum sum = new IntervalSum(list); + for (int i = 0; i<1000000; i++) { + final int pos = generator.nextInt(100); + final int val = generator.nextInt(2000000) - 1000000; sum.set(pos, val); list.set(pos, val); assertEquals(val, sum.get(pos)); } int s = 0; - List prefSum = new ArrayList<>(); + final List prefSum = new ArrayList(); prefSum.add(s); - for(Integer val: list) { + for (Integer val: list) { s += val; prefSum.add(s); } - for(int i = 0; i<=100; i++) { - for(int j = i; j<=100; j++) { + for (int i = 0; i<100; i++) { + for (int j = i; j<100; j++) { assertEquals(prefSum.get(j+1) - prefSum.get(i), sum.sum(i, j)); } } @@ -52,7 +55,7 @@ public void randomGeneratedTest() { @Test public void setIndexOutOfRangeTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.set(101, 10); @@ -64,7 +67,7 @@ public void setIndexOutOfRangeTest() { @Test public void sumIndexOutOfRangeTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.sum(101); @@ -76,7 +79,7 @@ public void sumIndexOutOfRangeTest() { @Test public void endBeforeStartTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.sum(101, 100); @@ -85,5 +88,4 @@ public void endBeforeStartTest() { } assertTrue(thrown); } - -} \ No newline at end of file +} diff --git a/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java new file mode 100644 index 00000000..ae78eabe --- /dev/null +++ b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java @@ -0,0 +1,56 @@ +package com.jwetherell.algorithms.data_structures.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor; +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.NodesNotInSameTreeException; +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.TreeNode; + +public class LowestCommonAncestorTest { + + @Test + public void largeTreeTest() throws NodesNotInSameTreeException { + + final TreeNode root = new TreeNode(); + final TreeNode left = root.addChild(); + final TreeNode middle = root.addChild(); + final TreeNode right = root.addChild(); + + //long path + TreeNode v = left; + for (int i = 0; i<1000; i++) + v = v.addChild(); + TreeNode leftRight = left.addChild(); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(v, leftRight), left); + + for (int i = 0; i<2000; i++) { + leftRight = leftRight.addChild(); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(v, leftRight), left); + } + + assertEquals(LowestCommonAncestor.lowestCommonAncestor(middle, right), root); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, right), root); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, root), root); + + final TreeNode root2 = new TreeNode(); + boolean thrownException = false; + try { + LowestCommonAncestor.lowestCommonAncestor(v, root2); + } catch (NodesNotInSameTreeException e) { + thrownException = true; + } + assertTrue(thrownException); + + final TreeNode deepChild = v.addChild(101); + assertEquals(deepChild, root.find(101)); + assertTrue(root.contains(101)); + + assertNull(root.find(102)); + assertFalse(root.contains(102)); + } +} diff --git a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java index b4512e57..6f2be148 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; +import java.util.Iterator; import org.junit.Test; @@ -16,7 +17,7 @@ public class QueueTests { @Test public void testArrayQueue() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(2500); String aName = "Queue [array]"; Queue.ArrayQueue aQueue = new Queue.ArrayQueue(); @@ -26,11 +27,22 @@ public void testArrayQueue() { data.unsorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(aCollection, Integer.class, aName, data.unsorted, data.sorted, data.invalid)); + + // Specific test based on bug + aQueue = new Queue.ArrayQueue(); + for (int i = 0; i < 1024; i++) { + aQueue.offer(i); + } + aQueue.poll(); + aQueue.offer(1024); + Iterator it = aQueue.toQueue().iterator(); + while (it.hasNext()) + it.next(); } @Test public void testLinkedQueue() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(250); String lName = "Queue [linked]"; Queue.LinkedQueue lQueue = new Queue.LinkedQueue(); @@ -40,5 +52,15 @@ public void testLinkedQueue() { data.unsorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(lCollection, Integer.class, lName, data.unsorted, data.sorted, data.invalid)); + + lQueue = new Queue.LinkedQueue(); + for (int i = 0; i < 1024; i++) { + lQueue.offer(i); + } + lQueue.poll(); + lQueue.offer(1024); + Iterator it = lQueue.toQueue().iterator(); + while (it.hasNext()) + it.next(); } } diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java index 12e790a7..c4338d5a 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java @@ -78,6 +78,12 @@ private static > boolean addAndRemoveInOrder(Collection< return false; } + if (!IteratorTest.testIterator(collection.iterator())) { + System.err.println(name+" addAndRemoveInOrder iterator failed."); + Utils.handleError(data,collection); + return false; + } + for (int i = 0; i < data.length; i++) { Integer value = data[i]; T item = Utils.parseT(value, type); diff --git a/test/com/jwetherell/algorithms/graph/test/Graphs.java b/test/com/jwetherell/algorithms/graph/test/Graphs.java index e63e5602..6a17dcd0 100644 --- a/test/com/jwetherell/algorithms/graph/test/Graphs.java +++ b/test/com/jwetherell/algorithms/graph/test/Graphs.java @@ -361,7 +361,7 @@ private Graph.CostPathPair getIdealDirectedWithNegWeightsPathPair(Direc } @Test - public void testDijstraUndirected() { + public void testDijkstraUndirected() { final UndirectedGraph undirected = new UndirectedGraph(); final Graph.Vertex start = undirected.v1; final Graph.Vertex end = undirected.v5; @@ -372,13 +372,13 @@ public void testDijstraUndirected() { for (Graph.Vertex v : map1.keySet()) { final Graph.CostPathPair path1 = map1.get(v); final Graph.CostPathPair path2 = getIdealUndirectedPath(undirected).get(v); - assertTrue("Dijstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); + assertTrue("Dijkstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); } final Graph.CostPathPair pair1 = Dijkstra.getShortestPath(undirected.graph, start, end); assertTrue("No path from " + start.getValue() + " to " + end.getValue(), (pair1 != null)); - assertTrue("Dijstra's shortest path error. pair="+pair1+" pair="+getIdealUndirectedPathPair(undirected), pair1.equals(getIdealUndirectedPathPair(undirected))); + assertTrue("Dijkstra's shortest path error. pair="+pair1+" pair="+getIdealUndirectedPathPair(undirected), pair1.equals(getIdealUndirectedPathPair(undirected))); } } @@ -546,14 +546,14 @@ public void testDijkstraDirected() { for (Graph.Vertex v : map1.keySet()) { final Graph.CostPathPair path1 = map1.get(v); final Graph.CostPathPair path2 = getIdealDirectedPath(directed).get(v); - assertTrue("Dijstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); + assertTrue("Dijkstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); } final Graph.CostPathPair pair1 = Dijkstra.getShortestPath(directed.graph, start, end); assertTrue("No path from "+start.getValue()+" to "+end.getValue(), (pair1!=null)); // Compare pair - assertTrue("Dijstra's shortest path error. pair1="+pair1+" idealPathPair="+getIdealPathPair(directed), pair1.equals(getIdealPathPair(directed))); + assertTrue("Dijkstra's shortest path error. pair1="+pair1+" idealPathPair="+getIdealPathPair(directed), pair1.equals(getIdealPathPair(directed))); } @Test @@ -579,7 +579,7 @@ public void testBellmanFordDirected() { } @Test - public void testDijstraDirectedWihtNegativeWeights() { + public void testDijkstraDirectedWihtNegativeWeights() { final DirectedWithNegativeWeights directedWithNegWeights = new DirectedWithNegativeWeights(); { // DIRECTED GRAPH (WITH NEGATIVE WEIGHTS) final Graph.Vertex start = directedWithNegWeights.v1; diff --git a/test/com/jwetherell/algorithms/numbers/test/Numbers.java b/test/com/jwetherell/algorithms/numbers/test/Numbers.java index dfe714cd..dcf087ab 100644 --- a/test/com/jwetherell/algorithms/numbers/test/Numbers.java +++ b/test/com/jwetherell/algorithms/numbers/test/Numbers.java @@ -91,17 +91,17 @@ public void testToEnglish() { assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = 199; - check = "one-hundred ninty-nine"; + check = "one-hundred ninety-nine"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = Integer.MAX_VALUE; // 2,147,483,647 - check = "two-billion one-hundred fourty-seven-million four-hundred eighty-three-thousand six-hundred fourty-seven"; + check = "two-billion one-hundred forty-seven-million four-hundred eighty-three-thousand six-hundred forty-seven"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = Integer.MIN_VALUE+1; // -2,147,483,647 - check = "negative two-billion one-hundred fourty-seven-million four-hundred eighty-three-thousand six-hundred fourty-seven"; + check = "negative two-billion one-hundred forty-seven-million four-hundred eighty-three-thousand six-hundred forty-seven"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); } diff --git a/test/com/jwetherell/algorithms/sequence/test/Sequences.java b/test/com/jwetherell/algorithms/sequence/test/Sequences.java index 18b7d0c1..bd3fa1d8 100644 --- a/test/com/jwetherell/algorithms/sequence/test/Sequences.java +++ b/test/com/jwetherell/algorithms/sequence/test/Sequences.java @@ -141,7 +141,7 @@ public void testLongestIncreasingSubsequence() { sequencesLis.add(new int[]{1, 2, 3}); sequences.add(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}); - sequencesLis.add(new int[]{0, 1, 3, 7, 11, 15}); + sequencesLis.add(new int[]{0, 2, 6, 9, 11, 15}); assertTrue("Longest increasing subsequence error. Sequences size=" + sequences.size() + " SequencesList size:" + sequencesLis.size(), sequences.size() == sequencesLis.size());