diff --git a/src/main/java/com/conversions/AnyBaseToDecimal.java b/src/main/java/com/conversions/AnyBaseToDecimal.java index 4267e662..65f89f29 100644 --- a/src/main/java/com/conversions/AnyBaseToDecimal.java +++ b/src/main/java/com/conversions/AnyBaseToDecimal.java @@ -3,6 +3,7 @@ package src.main.java.com.conversions; public class AnyBaseToDecimal { /** * This method produces a decimal value of any given input number of any base + * * @param inpNum String of which we need the decimal value and base in integer format * @return string format of the decimal value */ @@ -12,11 +13,11 @@ public class AnyBaseToDecimal { int num = 0; int pow = 1; - for (int i=len-1; i>=0; i--) { + for (int i = len - 1; i >= 0; i--) { if (valOfChar(inpNum.charAt(i)) >= base) { return "Invalid Number"; } - num += valOfChar(inpNum.charAt(i))*pow; + num += valOfChar(inpNum.charAt(i)) * pow; pow *= base; } return String.valueOf(num); @@ -24,16 +25,16 @@ public class AnyBaseToDecimal { /** * This method produces integer value of the input character and returns it + * * @param c Char of which we need the integer value of * @return integer value of input char */ private static int valOfChar(char c) { if (c >= '0' && c <= '9') { - return (int)c - '0'; - } - else { - return (int)c - 'A' + 10; + return (int) c - '0'; + } else { + return (int) c - 'A' + 10; } } } \ No newline at end of file diff --git a/src/main/java/com/conversions/BinaryToHexadecimal.java b/src/main/java/com/conversions/BinaryToHexadecimal.java index 8497fa22..fc891570 100644 --- a/src/main/java/com/conversions/BinaryToHexadecimal.java +++ b/src/main/java/com/conversions/BinaryToHexadecimal.java @@ -11,7 +11,7 @@ public class BinaryToHexadecimal { * hm to store hexadecimal codes for binary numbers * within the range: 0000 to 1111 i.e. for decimal numbers 0 to 15 */ - private static Map hmHexadecimal = new HashMap<>(16); + private static Map hmHexadecimal = new HashMap<>(16); static { int i; diff --git a/src/main/java/com/conversions/DecimalToAnyBase.java b/src/main/java/com/conversions/DecimalToAnyBase.java index ae675f23..912d423d 100644 --- a/src/main/java/com/conversions/DecimalToAnyBase.java +++ b/src/main/java/com/conversions/DecimalToAnyBase.java @@ -3,23 +3,24 @@ package src.main.java.com.conversions; import java.util.ArrayList; public class DecimalToAnyBase { + /** * This method produces a String value of any given input decimal in any base - * @param inp Decimal of which we need the value in base in String format + * + * @param inp Decimal of which we need the value in base in String format * @param base base in which we want the decimal value to be converted into * @return string format of the converted value in the given base */ - public String convertToAnyBase(int inp, int base) { ArrayList charArr = new ArrayList<>(); while (inp > 0) { - charArr.add(reVal(inp%base)); + charArr.add(reVal(inp % base)); inp /= base; } StringBuilder str = new StringBuilder(charArr.size()); - for(Character ch: charArr) { + for (Character ch : charArr) { str.append(ch); } @@ -28,14 +29,15 @@ public class DecimalToAnyBase { /** * This method produces character value of the input integer and returns it + * * @param num integer of which we need the character value of * @return character value of input integer */ private char reVal(int num) { if (num >= 0 && num <= 9) - return (char)(num + '0'); + return (char) (num + '0'); else - return (char)(num - 10 + 'A'); + return (char) (num - 10 + 'A'); } } diff --git a/src/main/java/com/conversions/DecimalToHexadecimal.java b/src/main/java/com/conversions/DecimalToHexadecimal.java index cce26666..e0e9360d 100644 --- a/src/main/java/com/conversions/DecimalToHexadecimal.java +++ b/src/main/java/com/conversions/DecimalToHexadecimal.java @@ -3,15 +3,16 @@ package src.main.java.com.conversions; import java.math.BigInteger; public class DecimalToHexadecimal { - private static final char hexChars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static final BigInteger valueHex = new BigInteger("16"); /** * This method converts and decimal number to a Hexadecimal number + * * @param decimalStr * @return hexadecimal number */ - public String decimalToHex(String decimalStr){ + public String decimalToHex(String decimalStr) { BigInteger decimal = new BigInteger(decimalStr); int rem; diff --git a/src/main/java/com/conversions/DecimalToOctal.java b/src/main/java/com/conversions/DecimalToOctal.java index 743b0e0a..6ab6e72b 100644 --- a/src/main/java/com/conversions/DecimalToOctal.java +++ b/src/main/java/com/conversions/DecimalToOctal.java @@ -3,20 +3,21 @@ package src.main.java.com.conversions; import java.math.BigInteger; public class DecimalToOctal { - private static final char octalChars[] = {'0','1','2','3','4','5','6','7'}; + private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'}; private static final BigInteger valueOctal = new BigInteger("8"); /** * This method converts and decimal number to a octal number + * * @param decimalStr * @return octal number */ - public String decimalToOctal(String decimalStr){ + public String decimalToOctal(String decimalStr) { BigInteger decimal = new BigInteger(decimalStr); int rem; String octal = ""; - while(decimal.compareTo(BigInteger.ZERO) > 0) { + while (decimal.compareTo(BigInteger.ZERO) > 0) { rem = decimal.mod(valueOctal).intValueExact(); octal = octalChars[rem] + octal; decimal = decimal.divide(valueOctal); diff --git a/src/main/java/com/dataStructures/BinaryTree.java b/src/main/java/com/dataStructures/BinaryTree.java index 8cdb319c..3542bce5 100644 --- a/src/main/java/com/dataStructures/BinaryTree.java +++ b/src/main/java/com/dataStructures/BinaryTree.java @@ -2,39 +2,42 @@ package src.main.java.com.dataStructures; /** * Binary tree for general value type, without redundancy - * @author RICARDO + * * @param root data */ + public class BinaryTree { private final T data; - private BinaryTree right, // the upper binary tree - left; // the lower binary tree + private BinaryTree right, // the upper binary tree + left; // the lower binary tree public BinaryTree(T data) { this.data = data; } @Override - public String toString(){ + public String toString() { return this.data.toString(); } - + /** * inserts a new value in it's correspondant place - * @param newDataValue value of the new banary tree to add on this tree + * + * @param newDataValue value of the new binary tree to add on this tree */ - public void insert(T newDataValue){ + public void insert(T newDataValue) { this.insert(new BinaryTree(newDataValue)); } - + /** * inserts a new binary tree in it's correspondant place - * @param newData new value to add on this tree + * + * @param newData new value to add on this tree */ - public void insert(BinaryTree newData){ - + public void insert(BinaryTree newData) { + int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value - + if (cpr < 0) if (this.left == null) this.setLeft(newData); @@ -51,12 +54,13 @@ public class BinaryTree { /** * search and specific value on the tree - * @param data Searched value - * @return Binary tree wich contains the value, null if it doesn't exist + * + * @param data Searched value + * @return Binary tree which contains the value, null if it doesn't exist */ - public BinaryTree search(T data){ + public BinaryTree search(T data) { int cpr = data.compareTo(this.data); //new value comparission respect to actual value - + if (cpr < 0) { if (this.left == null) return null; //the value doesn't exist @@ -65,43 +69,45 @@ public class BinaryTree { if (cpr > 0) { if (this.right == null) return null; //the value doesn't exist - return this.right.search(data); + return this.right.search(data); } return this; } - + /** * Checks if the data value exist in the tree + * * @param data data to be searched * @return true if this tree contains the data value, false if not. */ - public boolean contains(T data){ + public boolean contains(T data) { return this.search(data) != null; } - + /** * uses recursive black magic to print this tree in console + * * @param tabCounter prev tabs */ - private void print(int tabCounter){ + private void print(int tabCounter) { for (int i = 0; i < tabCounter; i++) System.out.print("\t"); - + System.out.println(this); - + if (this.left != null) this.left.print(tabCounter + 1); //it can't be ++ , pls don't change it if (this.right != null) this.right.print(tabCounter + 1); //it can't be ++ , pls don't change it } - + /** * uses black magic to print this tree in console */ - public void print(){ + public void print() { this.print(0); } - + //getters and setters public T getData() { return data; diff --git a/src/main/java/com/dataStructures/DisjointSet.java b/src/main/java/com/dataStructures/DisjointSet.java index c2a59c32..3fb70f6c 100644 --- a/src/main/java/com/dataStructures/DisjointSet.java +++ b/src/main/java/com/dataStructures/DisjointSet.java @@ -15,7 +15,6 @@ import java.util.*; *

* 2. quickly query two elements whether contained in the same set, requiring about O(1) time. * - * @author yangxf */ public class DisjointSet implements Serializable { private static final long serialVersionUID = 3134700471905625636L; diff --git a/src/main/java/com/generation/SimplexNoise.java b/src/main/java/com/generation/SimplexNoise.java index 699e0a2f..a4fcb3f1 100644 --- a/src/main/java/com/generation/SimplexNoise.java +++ b/src/main/java/com/generation/SimplexNoise.java @@ -7,128 +7,131 @@ import java.util.Random; */ public class SimplexNoise { - private SimplexNoiseOctave[] octaves; - private double[] frequencys; - private double[] amplitudes; - private int largestFeature; - private double persistance; - private long seed; - - /** - * @param largestFeature the diameter of the largest possible "cloud". - * @param persistence the persistence. a low persistence causes smoother transition between the features while a high one makes the transition hard. (range = {@code 0.0F} - {@code 1.0F}) - * @param seed the seed this algorithm will use to generate pseudo random numbers. The generation will always look the same if the seed and the other parameters have the same value as in a previous generation - */ - public SimplexNoise(int largestFeature, double persistence, long seed) { - - this.largestFeature = largestFeature; - this.persistance = persistence; - this.seed = seed; - - int octaveCount = (int)Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D)); - this.octaves = new SimplexNoiseOctave[octaveCount]; - this.frequencys = new double[octaveCount]; - this.amplitudes = new double[octaveCount]; - - Random random = new Random(seed); - - for(int index = 0; index < octaveCount; index++) { - - this.octaves[index] = new SimplexNoiseOctave(random.nextInt()); - this.frequencys[index] = Math.pow(2, index); - this.amplitudes[index] = Math.pow(persistence, octaveCount - index); - } - } - - /** - * Generates a height map. - * @param x X coordinate - * @param y Y coordinate - * @param width width - * @param height height - * @return the generated height map - */ - public float[][] generateHeightMap(int x, int y, int width, int height) { - - int xEnd = x + width; - int yEnd = y + height; - - float[][] result = new float[width][height]; - - for(int i = 0; i < width; i++) { - - for(int j = 0; j < height; j++) { - - int posX = x + i * ((xEnd - x) / width); - int posY = y + j * ((yEnd - y) / height); - result[i][j] = Math.min(1.0F, Math.max(0.0F, (float)(0.5D * (1 + this.getNoise(posX, posY))))); - } - } - - return result; - } - - /** - * Generates a two dimensional noise. - * @param x X coordinate - * @param y Y coordinate - * @return the generated noise - */ - public double getNoise(int x, int y) { - - double result = 0; - - for(int index = 0; index < this.octaves.length; index++) { - - result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index]; - } - - return result; - } - - /** - * Generates a three dimensional noise. - * @param x X coordinate - * @param y Y coordinate - * @param z Z coordinate - * @return the generated noise - */ - public double getNoise(int x, int y, int z) { - - double result = 0; - - for(int index = 0; index < this.octaves.length; index++) { - - double frequency = Math.pow(2, index); - double amplitude = Math.pow(this.persistance, this.octaves.length - index); - - result += this.octaves[index].noise(x / frequency, y / frequency, z / frequency) * amplitude; - } - - return result; - } - - /** - * @return the largest possible feature - */ - public int getLargestFeature() { - - return this.largestFeature; - } - - /** - * @return the persistence - */ - public double getPersistance() { - - return this.persistance; - } - - /** - * @return the seed - */ - public long getSeed() { - - return this.seed; - } + private SimplexNoiseOctave[] octaves; + private double[] frequencys; + private double[] amplitudes; + private int largestFeature; + private double persistance; + private long seed; + + /** + * @param largestFeature the diameter of the largest possible "cloud". + * @param persistence the persistence. a low persistence causes smoother transition between the features while a high one makes the transition hard. (range = {@code 0.0F} - {@code 1.0F}) + * @param seed the seed this algorithm will use to generate pseudo random numbers. The generation will always look the same if the seed and the other parameters have the same value as in a previous generation + */ + public SimplexNoise(int largestFeature, double persistence, long seed) { + + this.largestFeature = largestFeature; + this.persistance = persistence; + this.seed = seed; + + int octaveCount = (int) Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D)); + this.octaves = new SimplexNoiseOctave[octaveCount]; + this.frequencys = new double[octaveCount]; + this.amplitudes = new double[octaveCount]; + + Random random = new Random(seed); + + for (int index = 0; index < octaveCount; index++) { + + this.octaves[index] = new SimplexNoiseOctave(random.nextInt()); + this.frequencys[index] = Math.pow(2, index); + this.amplitudes[index] = Math.pow(persistence, octaveCount - index); + } + } + + /** + * Generates a height map. + * + * @param x X coordinate + * @param y Y coordinate + * @param width width + * @param height height + * @return the generated height map + */ + public float[][] generateHeightMap(int x, int y, int width, int height) { + + int xEnd = x + width; + int yEnd = y + height; + + float[][] result = new float[width][height]; + + for (int i = 0; i < width; i++) { + + for (int j = 0; j < height; j++) { + + int posX = x + i * ((xEnd - x) / width); + int posY = y + j * ((yEnd - y) / height); + result[i][j] = Math.min(1.0F, Math.max(0.0F, (float) (0.5D * (1 + this.getNoise(posX, posY))))); + } + } + + return result; + } + + /** + * Generates a two dimensional noise. + * + * @param x X coordinate + * @param y Y coordinate + * @return the generated noise + */ + public double getNoise(int x, int y) { + + double result = 0; + + for (int index = 0; index < this.octaves.length; index++) { + + result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index]; + } + + return result; + } + + /** + * Generates a three dimensional noise. + * + * @param x X coordinate + * @param y Y coordinate + * @param z Z coordinate + * @return the generated noise + */ + public double getNoise(int x, int y, int z) { + + double result = 0; + + for (int index = 0; index < this.octaves.length; index++) { + + double frequency = Math.pow(2, index); + double amplitude = Math.pow(this.persistance, this.octaves.length - index); + + result += this.octaves[index].noise(x / frequency, y / frequency, z / frequency) * amplitude; + } + + return result; + } + + /** + * @return the largest possible feature + */ + public int getLargestFeature() { + + return this.largestFeature; + } + + /** + * @return the persistence + */ + public double getPersistance() { + + return this.persistance; + } + + /** + * @return the seed + */ + public long getSeed() { + + return this.seed; + } } diff --git a/src/main/java/com/generation/SimplexNoiseOctave.java b/src/main/java/com/generation/SimplexNoiseOctave.java index 75d979e0..b1e281d2 100644 --- a/src/main/java/com/generation/SimplexNoiseOctave.java +++ b/src/main/java/com/generation/SimplexNoiseOctave.java @@ -14,7 +14,7 @@ public class SimplexNoiseOctave { new Gradient(1, 0, -1), new Gradient(-1, 0, -1), new Gradient(0, 1, 1), new Gradient(0, -1, 1), new Gradient(0, 1, -1), new Gradient(0, -1, -1) }; - private static final short P_SUPPLY[] = { + private static final short[] P_SUPPLY = { 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, diff --git a/src/main/java/com/matchings/stableMatching/GaleShapley.java b/src/main/java/com/matchings/stableMatching/GaleShapley.java index 3c60e836..18624f9b 100644 --- a/src/main/java/com/matchings/stableMatching/GaleShapley.java +++ b/src/main/java/com/matchings/stableMatching/GaleShapley.java @@ -5,8 +5,8 @@ public class GaleShapley { /** * Return a stable matching between men and women according to their preferences, * following the Gale-Shapley algorithm. - * - * @param menPrefs for each man, for each preference rank, the corresponding woman + * + * @param menPrefs for each man, for each preference rank, the corresponding woman * @param womenPrefs for each woman, for each preference rank, the corresponding man * @return for each man, the associated woman (1-dimensional array) */ @@ -88,7 +88,7 @@ public class GaleShapley { /** * Get a currently unengaged man, if there is one - * + * * @param menMatching the current men matching array (being constructed) * @return the first man that is not engaged, or -1 if all men are engaged */ diff --git a/src/main/java/com/search/BloomFilter.java b/src/main/java/com/search/BloomFilter.java index 122a3029..70a7f1c3 100644 --- a/src/main/java/com/search/BloomFilter.java +++ b/src/main/java/com/search/BloomFilter.java @@ -16,7 +16,6 @@ import java.util.function.Function; *

* The accuracy rate depends on capacity and hash functions. * - * @author yangxf */ public class BloomFilter implements Serializable { private static final long serialVersionUID = -4466610350741278658L; diff --git a/src/main/java/com/search/LinearSearch.java b/src/main/java/com/search/LinearSearch.java index e8e8e3e1..66db8aad 100644 --- a/src/main/java/com/search/LinearSearch.java +++ b/src/main/java/com/search/LinearSearch.java @@ -15,7 +15,7 @@ public final class LinearSearch { * @param is any comparable type * @return index of the element */ - public static > int findIndex(T array[], T key) { + public static > int findIndex(T[] array, T key) { return search(array, key); } @@ -24,7 +24,7 @@ public final class LinearSearch { * @param key The element you are looking for * @return the location of the key or -1 if the element is not found **/ - private static > int search(T array[], T key){ + private static > int search(T[] array, T key){ for(int i = 0; i < array.length;i++) { if (array[i].compareTo(key) == 0){ return i; diff --git a/src/main/java/com/sorts/HeapSort.java b/src/main/java/com/sorts/HeapSort.java index a1cefd09..4135e5ba 100644 --- a/src/main/java/com/sorts/HeapSort.java +++ b/src/main/java/com/sorts/HeapSort.java @@ -9,107 +9,106 @@ import static src.main.java.com.sorts.SortUtils.swap; public class HeapSort { + private static class Heap> { + /** + * Array to store heap + */ + private T[] heap; - private static class Heap> { - /** - * Array to store heap - */ - private T[] heap; - - /** - * Constructor - * - * @param heap array of unordered integers - */ - Heap(T[] heap) { - this.heap = heap; - } - - /** - * Heapifies subtree from top as root to last as last child - * - * @param rootIndex index of root - * @param lastChild index of last child - */ - private void heapSubtree(int rootIndex, int lastChild) { - int leftIndex = rootIndex * 2 + 1; - int rightIndex = rootIndex * 2 + 2; - T root = heap[rootIndex]; - if (rightIndex <= lastChild) { - // if has right and left children - T left = heap[leftIndex]; - T right = heap[rightIndex]; - if (less(left, right) && less(left, root)) { - swap(heap, leftIndex, rootIndex); - heapSubtree(leftIndex, lastChild); - } else if (less(right, root)) { - swap(heap, rightIndex, rootIndex); - heapSubtree(rightIndex, lastChild); + /** + * Constructor + * + * @param heap array of unordered integers + */ + Heap(T[] heap) { + this.heap = heap; } - } else if (leftIndex <= lastChild) { - // if no right child, but has left child - T left = heap[leftIndex]; - if (less(left, root)) { - swap(heap, leftIndex, rootIndex); - heapSubtree(leftIndex, lastChild); + + /** + * Heapifies subtree from top as root to last as last child + * + * @param rootIndex index of root + * @param lastChild index of last child + */ + private void heapSubtree(int rootIndex, int lastChild) { + int leftIndex = rootIndex * 2 + 1; + int rightIndex = rootIndex * 2 + 2; + T root = heap[rootIndex]; + if (rightIndex <= lastChild) { + // if has right and left children + T left = heap[leftIndex]; + T right = heap[rightIndex]; + if (less(left, right) && less(left, root)) { + swap(heap, leftIndex, rootIndex); + heapSubtree(leftIndex, lastChild); + } else if (less(right, root)) { + swap(heap, rightIndex, rootIndex); + heapSubtree(rightIndex, lastChild); + } + } else if (leftIndex <= lastChild) { + // if no right child, but has left child + T left = heap[leftIndex]; + if (less(left, root)) { + swap(heap, leftIndex, rootIndex); + heapSubtree(leftIndex, lastChild); + } + } } - } + + + /** + * Makes heap with root as root + * + * @param root index of root of heap + */ + private void makeMinHeap(int root) { + int leftIndex = root * 2 + 1; + int rightIndex = root * 2 + 2; + boolean hasLeftChild = leftIndex < heap.length; + boolean hasRightChild = rightIndex < heap.length; + if (hasRightChild) { + //if has left and right + makeMinHeap(leftIndex); + makeMinHeap(rightIndex); + heapSubtree(root, heap.length - 1); + } else if (hasLeftChild) { + heapSubtree(root, heap.length - 1); + } + } + + /** + * Gets the root of heap + * + * @return root of heap + */ + private T getRoot(int size) { + swap(heap, 0, size); + heapSubtree(0, size - 1); + // return old root + return heap[size]; + } + + } - - /** - * Makes heap with root as root - * - * @param root index of root of heap - */ - private void makeMinHeap(int root) { - int leftIndex = root * 2 + 1; - int rightIndex = root * 2 + 2; - boolean hasLeftChild = leftIndex < heap.length; - boolean hasRightChild = rightIndex < heap.length; - if (hasRightChild) { - //if has left and right - makeMinHeap(leftIndex); - makeMinHeap(rightIndex); - heapSubtree(root, heap.length - 1); - } else if (hasLeftChild) { - heapSubtree(root, heap.length - 1); - } + public > T[] sort(T[] unsorted) { + return sort(Arrays.asList(unsorted)).toArray(unsorted); } - /** - * Gets the root of heap - * - * @return root of heap - */ - private T getRoot(int size) { - swap(heap, 0, size); - heapSubtree(0, size - 1); - // return old root - return heap[size]; + private > List sort(List unsorted) { + int size = unsorted.size(); + + @SuppressWarnings("unchecked") + Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()])); + + // make min heap using index 0 as root. + heap.makeMinHeap(0); + List sorted = new ArrayList<>(size); + while (size > 0) { + T min = heap.getRoot(--size); + sorted.add(min); + } + + return sorted; } - - - } - - public > T[] sort(T[] unsorted) { - return sort(Arrays.asList(unsorted)).toArray(unsorted); - } - - private > List sort(List unsorted) { - int size = unsorted.size(); - - @SuppressWarnings("unchecked") - Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()])); - - // make min heap using index 0 as root. - heap.makeMinHeap(0); - List sorted = new ArrayList<>(size); - while (size > 0) { - T min = heap.getRoot(--size); - sorted.add(min); - } - - return sorted; - } } \ No newline at end of file diff --git a/src/main/java/com/sorts/MergeSort.java b/src/main/java/com/sorts/MergeSort.java index 3582a08b..6d6516b2 100644 --- a/src/main/java/com/sorts/MergeSort.java +++ b/src/main/java/com/sorts/MergeSort.java @@ -2,74 +2,74 @@ package src.main.java.com.sorts; public class MergeSort { - /** - * This method implements the Generic Merge Sort - * - * @param unsorted the array which should be sorted - * @param Comparable class - * @return sorted array - */ - @SuppressWarnings("unchecked") - public > T[] sort(T[] unsorted) { - T[] tmp = (T[]) new Comparable[unsorted.length]; - doSort(unsorted, tmp, 0, unsorted.length - 1); - return unsorted; - } - - /** - * @param arr The array to be sorted - * @param temp The copy of the actual array - * @param left The first index of the array - * @param right The last index of the array - * Recursively sorts the array in increasing order - **/ - private static > void doSort(T[] arr, T[] temp, int left, int right) { - if (left < right) { - int mid = left + (right - left) / 2; - doSort(arr, temp, left, mid); - doSort(arr, temp, mid + 1, right); - merge(arr, temp, left, mid, right); - } - } - - /** - * This method implements the merge step of the merge sort - * - * @param arr The array to be sorted - * @param temp The copy of the actual array - * @param left The first index of the array - * @param mid The middle index of the array - * @param right The last index of the array - * merges two parts of an array in increasing order - **/ - private static > void merge(T[] arr, T[] temp, int left, int mid, int right) { - System.arraycopy(arr, left, temp, left, right - left + 1); - - int i = left; - int j = mid + 1; - int k = left; - - while (i <= mid && j <= right) { - if (temp[i].compareTo(temp[j]) <= 0) { - arr[k] = temp[i]; - i++; - } else { - arr[k] = temp[j]; - j++; - } - k++; + /** + * This method implements the Generic Merge Sort + * + * @param unsorted the array which should be sorted + * @param Comparable class + * @return sorted array + */ + @SuppressWarnings("unchecked") + public > T[] sort(T[] unsorted) { + T[] tmp = (T[]) new Comparable[unsorted.length]; + doSort(unsorted, tmp, 0, unsorted.length - 1); + return unsorted; } - while (i <= mid) { - arr[k] = temp[i]; - i++; - k++; + /** + * @param arr The array to be sorted + * @param temp The copy of the actual array + * @param left The first index of the array + * @param right The last index of the array + * Recursively sorts the array in increasing order + **/ + private static > void doSort(T[] arr, T[] temp, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + doSort(arr, temp, left, mid); + doSort(arr, temp, mid + 1, right); + merge(arr, temp, left, mid, right); + } } - while (j <= right) { - arr[k] = temp[j]; - j++; - k++; + /** + * This method implements the merge step of the merge sort + * + * @param arr The array to be sorted + * @param temp The copy of the actual array + * @param left The first index of the array + * @param mid The middle index of the array + * @param right The last index of the array + * merges two parts of an array in increasing order + **/ + private static > void merge(T[] arr, T[] temp, int left, int mid, int right) { + System.arraycopy(arr, left, temp, left, right - left + 1); + + int i = left; + int j = mid + 1; + int k = left; + + while (i <= mid && j <= right) { + if (temp[i].compareTo(temp[j]) <= 0) { + arr[k] = temp[i]; + i++; + } else { + arr[k] = temp[j]; + j++; + } + k++; + } + + while (i <= mid) { + arr[k] = temp[i]; + i++; + k++; + } + + while (j <= right) { + arr[k] = temp[j]; + j++; + k++; + } } - } } diff --git a/src/main/java/com/sorts/QuickSort.java b/src/main/java/com/sorts/QuickSort.java index 31d6a0fb..e1a5f15d 100644 --- a/src/main/java/com/sorts/QuickSort.java +++ b/src/main/java/com/sorts/QuickSort.java @@ -5,61 +5,60 @@ import static src.main.java.com.sorts.SortUtils.swap; public class QuickSort { - - /** - * This method implements the Generic Quick Sort - * - * @param array The array to be sorted - * Sorts the array in increasing order - **/ - public > T[] sort(T[] array) { - doSort(array, 0, array.length - 1); - return array; - } - - - /** - * The sorting process - * - * @param left The first index of an array - * @param right The last index of an array - * @param array The array to be sorted - **/ - - private static > void doSort(T[] array, int left, int right) { - if (left < right) { - int pivot = partition(array, left, right); - doSort(array, left, pivot - 1); - doSort(array, pivot, right); + /** + * This method implements the Generic Quick Sort + * + * @param array The array to be sorted + * Sorts the array in increasing order + **/ + public > T[] sort(T[] array) { + doSort(array, 0, array.length - 1); + return array; } - } - /** - * This method finds the partition index for an array - * - * @param array The array to be sorted - * @param left The first index of an array - * @param right The last index of an array - * Finds the partition index of an array - **/ - private static > int partition(T[] array, int left, int right) { - int mid = (left + right) / 2; - T pivot = array[mid]; + /** + * The sorting process + * + * @param left The first index of an array + * @param right The last index of an array + * @param array The array to be sorted + **/ - while (left <= right) { - while (less(array[left], pivot)) { - ++left; - } - while (less(pivot, array[right])) { - --right; - } - if (left <= right) { - swap(array, left, right); - ++left; - --right; - } + private static > void doSort(T[] array, int left, int right) { + if (left < right) { + int pivot = partition(array, left, right); + doSort(array, left, pivot - 1); + doSort(array, pivot, right); + } + } + + /** + * This method finds the partition index for an array + * + * @param array The array to be sorted + * @param left The first index of an array + * @param right The last index of an array + * Finds the partition index of an array + **/ + + private static > int partition(T[] array, int left, int right) { + int mid = (left + right) / 2; + T pivot = array[mid]; + + while (left <= right) { + while (less(array[left], pivot)) { + ++left; + } + while (less(pivot, array[right])) { + --right; + } + if (left <= right) { + swap(array, left, right); + ++left; + --right; + } + } + return left; } - return left; - } } \ No newline at end of file diff --git a/src/main/java/com/sorts/SelectionSort.java b/src/main/java/com/sorts/SelectionSort.java index 1038bec3..b498aa79 100644 --- a/src/main/java/com/sorts/SelectionSort.java +++ b/src/main/java/com/sorts/SelectionSort.java @@ -5,28 +5,28 @@ import static src.main.java.com.sorts.SortUtils.swap; public class SelectionSort { - /** - * This method implements the Generic Selection Sort - * - * @param arr The array to be sorted - * Sorts the array in increasing order - **/ - public > T[] sort(T[] arr) { - int n = arr.length; - for (int i = 0; i < n - 1; i++) { - // Initial index of min - int min = i; - for (int j = i + 1; j < n; j++) { - if (less(arr[j], arr[min])) { - min = j; + /** + * This method implements the Generic Selection Sort + * + * @param arr The array to be sorted + * Sorts the array in increasing order + **/ + public > T[] sort(T[] arr) { + int n = arr.length; + for (int i = 0; i < n - 1; i++) { + // Initial index of min + int min = i; + for (int j = i + 1; j < n; j++) { + if (less(arr[j], arr[min])) { + min = j; + } + } + // Swapping if index of min is changed + if (min != i) { + swap(arr, i, min); + } } - } - // Swapping if index of min is changed - if (min != i) { - swap(arr, i, min); - } - } - return arr; - } + return arr; + } } \ No newline at end of file diff --git a/src/main/java/com/sorts/ShellSort.java b/src/main/java/com/sorts/ShellSort.java index d300cfc0..8ec8e415 100644 --- a/src/main/java/com/sorts/ShellSort.java +++ b/src/main/java/com/sorts/ShellSort.java @@ -5,28 +5,28 @@ import static src.main.java.com.sorts.SortUtils.swap; public class ShellSort { - /** - * This method implements Generic Shell Sort. - * - * @param array The array to be sorted - */ - public > T[] sort(T[] array) { - int length = array.length; - int n = 1; + /** + * This method implements Generic Shell Sort. + * + * @param array The array to be sorted + */ + public > T[] sort(T[] array) { + int length = array.length; + int n = 1; - while (n < length / 3) { - n = 3 * n + 1; - } - - while (n >= 1) { - for (int i = n; i < length; i++) { - for (int j = i; j >= n && less(array[j], array[j - n]); j -= n) { - swap(array, j, j - n); + while (n < length / 3) { + n = 3 * n + 1; } - } - n /= 3; - } - return array; - } + while (n >= 1) { + for (int i = n; i < length; i++) { + for (int j = i; j >= n && less(array[j], array[j - n]); j -= n) { + swap(array, j, j - n); + } + } + n /= 3; + } + + return array; + } } \ No newline at end of file diff --git a/src/main/java/com/sorts/SortUtils.java b/src/main/java/com/sorts/SortUtils.java index 26fc683e..bd5e5a65 100644 --- a/src/main/java/com/sorts/SortUtils.java +++ b/src/main/java/com/sorts/SortUtils.java @@ -3,43 +3,43 @@ package src.main.java.com.sorts; final class SortUtils { - /** - * Helper method for swapping places in array - * - * @param array The array which elements we want to swap - * @param idx index of the first element - * @param idy index of the second element - */ - static boolean swap(T[] array, int idx, int idy) { - T swap = array[idx]; - array[idx] = array[idy]; - array[idy] = swap; - return true; - } - - - /** - * This method checks if first element is less then the other element - * - * @param v first element - * @param w second element - * @return true if the first element is less then the second element - */ - static > boolean less(T v, T w) { - return v.compareTo(w) < 0; - } - - - /** - * Swaps all position from {@param left} to @{@param right} for {@param array} - * - * @param array is an array - * @param left is a left flip border of the array - * @param right is a right flip border of the array - */ - static > void flip(T[] array, int left, int right) { - while (left <= right) { - swap(array, left++, right--); + /** + * Helper method for swapping places in array + * + * @param array The array which elements we want to swap + * @param idx index of the first element + * @param idy index of the second element + */ + static boolean swap(T[] array, int idx, int idy) { + T swap = array[idx]; + array[idx] = array[idy]; + array[idy] = swap; + return true; + } + + + /** + * This method checks if first element is less then the other element + * + * @param v first element + * @param w second element + * @return true if the first element is less then the second element + */ + static > boolean less(T v, T w) { + return v.compareTo(w) < 0; + } + + + /** + * Swaps all position from {@param left} to @{@param right} for {@param array} + * + * @param array is an array + * @param left is a left flip border of the array + * @param right is a right flip border of the array + */ + static > void flip(T[] array, int left, int right) { + while (left <= right) { + swap(array, left++, right--); + } } - } } \ No newline at end of file diff --git a/src/main/java/com/sorts/StoogeSort.java b/src/main/java/com/sorts/StoogeSort.java index fe81a6df..b57d35df 100644 --- a/src/main/java/com/sorts/StoogeSort.java +++ b/src/main/java/com/sorts/StoogeSort.java @@ -5,35 +5,35 @@ import static src.main.java.com.sorts.SortUtils.less; public class StoogeSort { - /** - * This method implements recursion StoogeSort - * - * @param int[] array to store number elements - * @param f first element in the array - * @param l last element in the array - */ - public > T[] sort(T[] arr, int f, int l) { + /** + * This method implements recursion StoogeSort + * + * @param arr array to store number elements + * @param f first element in the array + * @param l last element in the array + */ + public > T[] sort(T[] arr, int f, int l) { - // Ends recursion when met - if (f >= l) - return arr; + // Ends recursion when met + if (f >= l) + return arr; - if (less(arr[l], arr[f])) { - swap(arr, f, l); - } + if (less(arr[l], arr[f])) { + swap(arr, f, l); + } - if (l - f + 1 > 2) { - int entry = (l - f + 1) / 3; + if (l - f + 1 > 2) { + int entry = (l - f + 1) / 3; - // Does a recursive sort of the first two thirds elements - sort(arr, f, l - entry); + // Does a recursive sort of the first two thirds elements + sort(arr, f, l - entry); - // Does a recursive sort of the last two thirds elements - sort(arr, f + entry, l); + // Does a recursive sort of the last two thirds elements + sort(arr, f + entry, l); - // Another recursive sort first two thirds elements to confirm - sort(arr, f, l - entry); - } - return arr; - } + // Another recursive sort first two thirds elements to confirm + sort(arr, f, l - entry); + } + return arr; + } } \ No newline at end of file diff --git a/src/main/java/com/types/Sort.java b/src/main/java/com/types/Sort.java index da89756f..e3316b4e 100644 --- a/src/main/java/com/types/Sort.java +++ b/src/main/java/com/types/Sort.java @@ -3,5 +3,5 @@ package src.main.java.com.types; @FunctionalInterface public interface Sort { - public > T[] sort(T[] array); + > T[] sort(T[] array); } diff --git a/src/test/java/com/dataStructures/BinaryTreeTest.java b/src/test/java/com/dataStructures/BinaryTreeTest.java index b269441d..383bc0aa 100644 --- a/src/test/java/com/dataStructures/BinaryTreeTest.java +++ b/src/test/java/com/dataStructures/BinaryTreeTest.java @@ -1,30 +1,29 @@ -package src.main.java.com.dataStructures; +package src.test.java.com.dataStructures; import org.junit.Test; +import src.main.java.com.dataStructures.BinaryTree; + import static org.junit.Assert.*; -/** - * - * @author RICARDO - */ public class BinaryTreeTest { - + public BinaryTreeTest() { } - + /** * Test of insert method, of class BinaryTree. */ @Test - public void testInsert_BinaryTree() { + public void testInsertBinaryTree() { System.out.println("insert"); - BinaryTree lowerdata = new BinaryTree<>("1"); - BinaryTree upperdata = new BinaryTree<>("3"); + BinaryTree lowerData = new BinaryTree<>("1"); + BinaryTree upperData = new BinaryTree<>("3"); BinaryTree instance = new BinaryTree<>("2"); - instance.insert(lowerdata); - instance.insert(upperdata); - String proof = instance.getLeft().toString()+instance.toString()+instance.getRight().toString(); - System.out.println(proof); + instance.insert(lowerData); + instance.insert(upperData); + String proof = instance.getLeft().toString() + + instance.toString() + + instance.getRight().toString(); assertEquals("123", proof); } @@ -36,10 +35,10 @@ public class BinaryTreeTest { System.out.println("search"); BinaryTree instance = new BinaryTree<>(5); for (int i = 1; i < 10; i++) { - instance.insert(new Integer(i)); + instance.insert(i); } - BinaryTree result = instance.search(new Integer(1)); - assertEquals(new Integer(1), result.getData()); + BinaryTree result = instance.search(1); + assertEquals(1, result.getData()); } /** @@ -52,9 +51,8 @@ public class BinaryTreeTest { for (int i = 1; i < 10; i++) { instance.insert(i); } - - boolean result = instance.contains(2)&&instance.contains(11); - assertEquals(false, result); - } + boolean result = instance.contains(2) && instance.contains(11); + assertFalse(result); + } }