diff --git a/Compression/bin/HEncoder$Node$Nodecomparator.class b/Compression/bin/HEncoder$Node$Nodecomparator.class new file mode 100644 index 000000000000..132ac19ff6ca Binary files /dev/null and b/Compression/bin/HEncoder$Node$Nodecomparator.class differ diff --git a/Compression/bin/HEncoder$Node.class b/Compression/bin/HEncoder$Node.class new file mode 100644 index 000000000000..9fcce4c5042a Binary files /dev/null and b/Compression/bin/HEncoder$Node.class differ diff --git a/Compression/bin/HEncoder.class b/Compression/bin/HEncoder.class new file mode 100644 index 000000000000..3e74095523fe Binary files /dev/null and b/Compression/bin/HEncoder.class differ diff --git a/Compression/bin/compressclient.class b/Compression/bin/compressclient.class new file mode 100644 index 000000000000..6465df8e705a Binary files /dev/null and b/Compression/bin/compressclient.class differ diff --git a/Compression/bin/genericheap.class b/Compression/bin/genericheap.class new file mode 100644 index 000000000000..16c2d03aa102 Binary files /dev/null and b/Compression/bin/genericheap.class differ diff --git a/Compression/src/HEncoder.java b/Compression/src/HEncoder.java new file mode 100644 index 000000000000..37bd8fc0a0f0 --- /dev/null +++ b/Compression/src/HEncoder.java @@ -0,0 +1,108 @@ +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; + +public class HEncoder { + + public HashMap encoder = new HashMap<>(); // in order to encode + public HashMap decoder = new HashMap<>(); // in order to decode + + private static class Node { + + Character ch; + Integer freq; + Node left; + Node right; + + public static final Nodecomparator Ctor = new Nodecomparator(); + + public static class Nodecomparator implements Comparator { + + @Override + public int compare(Node o1, Node o2) { + return o2.freq - o1.freq; + } + + } + } + + public HEncoder(String feeder) { + // 1. freq map + HashMap freqmap = new HashMap<>(); + for (int i = 0; i < feeder.length(); ++i) { + char ch = feeder.charAt(i); + if (freqmap.containsKey(ch)) { + freqmap.put(ch, freqmap.get(ch) + 1); + } else { + freqmap.put(ch, 1); + } + } + + // 2. prepare the heap from keyset + genericheap heap = new genericheap(Node.Ctor); + ArrayList k = new ArrayList<>(freqmap.keySet()); + for (Character c : k) { + Node n = new Node(); + n.ch = c; + n.left = null; + n.right = null; + n.freq = freqmap.get(c); + heap.add(n); + } + + // 3.Prepare tree, remove two , merge, add it back + Node fn = new Node(); + while (heap.size() != 1) { + Node n1 = heap.removeHP(); + Node n2 = heap.removeHP(); + fn = new Node(); + + fn.freq = n1.freq + n2.freq; + fn.left = n1; + fn.right = n2; + + heap.add(fn); + } + + // 4. traverse + + traverse(heap.removeHP(), ""); + } + + private void traverse(Node node, String osf) { + + if (node.left == null && node.right == null) { + encoder.put(node.ch, osf); + decoder.put(osf, node.ch); + return; + } + traverse(node.left, osf + "0"); + traverse(node.right, osf + "1"); + + } + + // compression work done here + public String compress(String str) { + String rv = ""; + for (int i = 0; i < str.length(); ++i) { + rv += encoder.get(str.charAt(i)); + } + return rv; + } + + + //in order to decompress + public String decompress(String str) { + String s = ""; + String code = ""; + for (int i = 0; i < str.length(); ++i) { + code += str.charAt(i); + if (decoder.containsKey(code)) { + s += decoder.get(code); + code = ""; + } + } + + return s; + } +} diff --git a/Compression/src/compressclient.java b/Compression/src/compressclient.java new file mode 100644 index 000000000000..496c8a849c82 --- /dev/null +++ b/Compression/src/compressclient.java @@ -0,0 +1,11 @@ + +public class compressclient { + + public static void main(String[] args) { + + HEncoder h= new HEncoder("aaaabbbcccccccccccdddd"); + System.out.println(h.compress("aabccd")); + System.out.println(h.decompress("101011000111")); + } + +} diff --git a/Compression/src/genericheap.java b/Compression/src/genericheap.java new file mode 100644 index 000000000000..de00316f0d53 --- /dev/null +++ b/Compression/src/genericheap.java @@ -0,0 +1,93 @@ + + +import java.util.ArrayList; +import java.util.Comparator; + +public class genericheap { // create a generic heap class , where T can be of any type. + + private ArrayList data = new ArrayList<>(); + private Comparator ctor; + + public genericheap(Comparator ctor) { // constructor to initialize the generic comparator + this.ctor=ctor; + } + + public int size() { // returns the size of the arraylist data + return data.size(); + } + + public boolean isEmpty() { // checks whether the list is empty or not :: return true or false for the same + return data.isEmpty(); + } + + public void display() { //displays the list + System.out.println(this.data); + } + + public void add(T integer) { // in this function we have added the type object into the arraylist and called upheapify + data.add(integer); + upheapify(data.size() - 1); + } + + private void upheapify(int ci) { + if (ci == 0) { + return; + } + int pi = (ci - 1) / 2; + if (isLarger(ci,pi) == true) { + swap(ci, pi); + upheapify(pi); + } + } + + private boolean isLarger(int i, int j) { + T ith = data.get(i); + T jth = data.get(j); + if(ctor.compare(ith,jth)>0) + { + return true; + } + else + { + return false; + } + } + + private void swap(int ci, int pi) { // swap function written like this because of the generic property + T ith = data.get(ci); + T jth=data.get(pi); + data.set(ci, jth); + data.set(pi, ith); + } + + public T getHP() { + return data.get(0); + } + + public T removeHP() { + + swap(0, data.size() - 1); + T rv=data.remove(data.size()-1); + downheapify(0); + return rv; + } + + private void downheapify(int pi) { + int lci = 2 * pi + 1; + int rci = 2 * pi + 2; + + int max = pi; + + if (lci < data.size() && isLarger(lci, max) == true) { + max = lci; + } + if (rci < data.size() && isLarger(rci, max) == true) { + max = rci; + } + if (max != pi) { + swap(pi, max); + downheapify(max); + } + } + +} diff --git a/Conversions/AnytoAny.java b/Conversions/AnytoAny.java new file mode 100644 index 000000000000..a86e2d042059 --- /dev/null +++ b/Conversions/AnytoAny.java @@ -0,0 +1,29 @@ +package Java.Conversions; + +import java.util.Scanner; +//given a source number , source base, destination base, this code can give you the destination number. +//sn ,sb,db ---> ()dn . this is what we have to do . + +public class AnytoAny { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + int sn = scn.nextInt(); + int sb = scn.nextInt(); + int db = scn.nextInt(); + int m = 1, dec = 0, dn = 0; + while (sn != 0) { + dec = dec + (sn % 10) * m; + m *= sb; + sn /= 10; + } + m = 1; + while (dec != 0) { + dn = dn + (dec % db) * m; + m *= 10; + dec /= db; + } + System.out.println(dn); + } + +} diff --git a/Data Structures/Stacks/Stacks.java b/Data Structures/Stacks/Stacks.java index 95469b91c4b3..2861ef5c17e8 100644 --- a/Data Structures/Stacks/Stacks.java +++ b/Data Structures/Stacks/Stacks.java @@ -43,6 +43,7 @@ public void push(int value){ stackArray[top] = value; }else{ resize(maxSize*2); + push(value);// don't forget push after resizing } } @@ -58,6 +59,7 @@ public int pop(){ if(top < maxSize/4){ resize(maxSize/2); + return pop();// don't forget pop after resizing } else{ System.out.println("The stack is already empty"); @@ -80,9 +82,11 @@ public int peek(){ } private void resize(int newSize){ - private int[] transferArray = new int[newSize]; + //private int[] transferArray = new int[newSize]; we can't put modifires here ! + int[] transferArray = new int[newSize]; - for(int i = 0; i < stackArray.length(); i++){ + //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . + for(int i = 0; i < stackArray.length; i++){ transferArray[i] = stackArray[i]; stackArray = transferArray; } diff --git a/Others/PerlinNoise.java b/Others/PerlinNoise.java new file mode 100644 index 000000000000..1d0f795a3a35 --- /dev/null +++ b/Others/PerlinNoise.java @@ -0,0 +1,164 @@ +import java.util.Random; +import java.util.Scanner; + +/** + * For detailed info and implementation see: Perlin-Noise + */ +public class PerlinNoise { + /** + * @param width width of noise array + * @param height height of noise array + * @param octaveCount numbers of layers used for blending noise + * @param persistence value of impact each layer get while blending + * @param seed used for randomizer + * @return float array containing calculated "Perlin-Noise" values + */ + static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) { + final float[][] base = new float[width][height]; + final float[][] perlinNoise = new float[width][height]; + final float[][][] noiseLayers = new float[octaveCount][][]; + + Random random = new Random(seed); + //fill base array with random values as base for noise + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + base[x][y] = random.nextFloat(); + } + } + + //calculate octaves with different roughness + for(int octave = 0; octave < octaveCount; octave++) { + noiseLayers[octave] = generatePerlinNoiseLayer(base, width, height, octave); + } + + float amplitude = 1f; + float totalAmplitude = 0f; + + //calculate perlin noise by blending each layer together with specific persistence + for(int octave = octaveCount - 1; octave >= 0; octave--) { + amplitude *= persistence; + totalAmplitude += amplitude; + + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + //adding each value of the noise layer to the noise + //by increasing amplitude the rougher noises will have more impact + perlinNoise[x][y] += noiseLayers[octave][x][y] * amplitude; + } + } + } + + //normalize values so that they stay between 0..1 + for(int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + perlinNoise[x][y] /= totalAmplitude; + } + } + + return perlinNoise; + } + + /** + * @param base base random float array + * @param width width of noise array + * @param height height of noise array + * @param octave current layer + * @return float array containing calculated "Perlin-Noise-Layer" values + */ + static float[][] generatePerlinNoiseLayer(float[][] base, int width, int height, int octave) { + float[][] perlinNoiseLayer = new float[width][height]; + + //calculate period (wavelength) for different shapes + int period = 1 << octave; //2^k + float frequency = 1f / period; // 1/2^k + + for(int x = 0; x < width; x++) { + //calculates the horizontal sampling indices + int x0 = (x / period) * period; + int x1 = (x0 + period) % width; + float horizintalBlend = (x - x0) * frequency; + + for(int y = 0; y < height; y++) { + //calculates the vertical sampling indices + int y0 = (y / period) * period; + int y1 = (y0 + period) % height; + float verticalBlend = (y - y0) * frequency; + + //blend top corners + float top = interpolate(base[x0][y0], base[x1][y0], horizintalBlend); + + //blend bottom corners + float bottom = interpolate(base[x0][y1], base[x1][y1], horizintalBlend); + + //blend top and bottom interpolation to get the final blend value for this cell + perlinNoiseLayer[x][y] = interpolate(top, bottom, verticalBlend); + } + } + + return perlinNoiseLayer; + } + + /** + * @param a value of point a + * @param b value of point b + * @param alpha determine which value has more impact (closer to 0 -> a, closer to 1 -> b) + * @return interpolated value + */ + static float interpolate(float a, float b, float alpha) { + return a * (1 - alpha) + alpha * b; + } + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + final int width; + final int height; + final int octaveCount; + final float persistence; + final long seed; + final String charset; + final float[][] perlinNoise; + + System.out.println("Width (int): "); + width = in.nextInt(); + + System.out.println("Height (int): "); + height = in.nextInt(); + + System.out.println("Octave count (int): "); + octaveCount = in.nextInt(); + + System.out.println("Persistence (float): "); + persistence = in.nextFloat(); + + System.out.println("Seed (long): "); + seed = in.nextLong(); + + System.out.println("Charset (String): "); + charset = in.next(); + + + perlinNoise = generatePerlinNoise(width, height, octaveCount, persistence, seed); + final char[] chars = charset.toCharArray(); + final int length = chars.length; + final float step = 1f / length; + //output based on charset + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + float value = step; + float noiseValue = perlinNoise[x][y]; + + for (char c : chars) { + if (noiseValue <= value) { + System.out.print(c); + break; + } + + value += step; + } + } + + System.out.println(); + } + } +} diff --git a/Sorts/CombSort.java b/Sorts/CombSort.java new file mode 100644 index 000000000000..0fc1fe781d9e --- /dev/null +++ b/Sorts/CombSort.java @@ -0,0 +1,67 @@ +/** +* +* @author Paul Parisot (https://github.com/tosirap) +* +*/ + +public class CombSort{ + /** + * This method implements the Generic comb Sort + * @param arr The array to be sorted + * Sorts the array in increasing order + **/ + + public static > void cS(T[] arr){ + int intervalle = arr.length; //initialisation of intervalle + boolean echange = true; //initialisation at true; + + while(intervalle > 1 || echange){ + intervalle /= 1.3; + if(intervalle < 1){ + intervalle = 1; + } + + int i = 0; + echange = false; + + while(i < (arr.length-intervalle)){ + if(arr[i].compareTo(arr[i+intervalle]) > 0){ + T temp = arr[i]; + arr[i] = arr[i+intervalle]; + arr[i+intervalle] = temp; + echange = true; + } + i++; + } + } + } + + public static void main(String[] args) { + + // Integer Input + int[] arr1 = {4,23,6,78,1,54,231,9,12}; + + Integer[] array = new Integer[arr1.length]; + for (int i = 0; i < arr1.length; i++){ + array[i] = arr1[i]; + } + + cS(array); + + // Output => 1 4 6 9 12 23 54 78 231 + for(int i = 0; i < array.length; i++){ + System.out.print(array[i]+"\t"); + } + System.out.println(); + + // String Input + String[] array1 = {"c", "a", "e", "b","d"}; + + cS(array1); + + //Output => a b c d e + for(int i = 0; i < array1.length; i++){ + System.out.print(array1[i]+"\t"); + } + } +} diff --git a/Sorts/OddEven.java b/Sorts/OddEven.java new file mode 100644 index 000000000000..206e32c7da50 --- /dev/null +++ b/Sorts/OddEven.java @@ -0,0 +1,62 @@ +/** +* +* @author Paul Parisot (https://github.com/tosirap) +* +*/ + +class OddEven{ + /** + * This method implements the Generic Odd Even Sort + * @param arr The array to be sorted + * Sorts the array in increasing order + **/ + + public static > void oE(T[] arr) { + boolean sorted = false; + while(!sorted){ + sorted = true; + for(int i = 1; i < arr.length-1; i += 2){ //Odd + if(arr[i].compareTo(arr[i+1]) > 0){ + T temp= arr[i]; //swap arr[i] and arr[i+1] + arr[i] = arr[i+1]; + arr[i+1] = temp; + sorted = false; + } + } + for(int i = 0; i < arr.length-1; i += 2){ //Even + if(arr[i].compareTo(arr[i+1]) > 0){ + T temp = arr[i]; //swap arr[i] and arr[i+1] + arr[i] = arr[i+1]; + arr[i+1] = temp; + sorted = false; + } + } + } + } + + public static void main(String[] args) { + // Integer Input + int[] arr1 = {4,23,6,78,1,54,231,9,12}; + Integer[] array = new Integer[arr1.length]; + for (int i = 0; i < arr1.length; i++){ + array[i] = arr1[i]; + } + + oE(array); + + // Output => 1 4 6 9 12 23 54 78 231 + for(int i = 0; i < array.length; i++){ + System.out.print(array[i]+"\t"); + } + System.out.println(); + + // String Input + String[] array1 = {"c", "a", "e", "b","d"}; + oE(array1); + + //Output => a b c d e + for(int i = 0; i