From ffa8d834c3272a4c72b3bcbbb6ed552ed6c98b84 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Tue, 3 Oct 2017 21:10:08 +0530 Subject: [PATCH 01/14] Add Compression Algorithm --- .../bin/HEncoder$Node$Nodecomparator.class | Bin 0 -> 859 bytes Compression/bin/HEncoder$Node.class | Bin 0 -> 683 bytes Compression/bin/HEncoder.class | Bin 0 -> 3674 bytes Compression/bin/compressclient.class | Bin 0 -> 756 bytes Compression/bin/genericheap.class | Bin 0 -> 2858 bytes Compression/src/HEncoder.java | 105 ++++++++++++++++++ Compression/src/compressclient.java | 11 ++ Compression/src/genericheap.java | 93 ++++++++++++++++ 8 files changed, 209 insertions(+) create mode 100644 Compression/bin/HEncoder$Node$Nodecomparator.class create mode 100644 Compression/bin/HEncoder$Node.class create mode 100644 Compression/bin/HEncoder.class create mode 100644 Compression/bin/compressclient.class create mode 100644 Compression/bin/genericheap.class create mode 100644 Compression/src/HEncoder.java create mode 100644 Compression/src/compressclient.java create mode 100644 Compression/src/genericheap.java diff --git a/Compression/bin/HEncoder$Node$Nodecomparator.class b/Compression/bin/HEncoder$Node$Nodecomparator.class new file mode 100644 index 0000000000000000000000000000000000000000..132ac19ff6cac8f89e2f9c0e4fbdab3088d97268 GIT binary patch literal 859 zcmaJfgc5PjpMapO9z6Bo)yDJ?BDUnK%@L8&UC2!y35Jrw1>ahKGUYs1FoS8=IS z6^R4l%#T9M+Wkr*aoBk?v$JpC?9QLRKYsyugFO=lVg2L#$d8Yu+8MCTj31wU5lSSn zGEpQrr{bIFgd!Su4o9cbPfU~ut64Tpg0R!OsV9{7gD6N22!(oMNGSH0)ItGEHcTuM zR{B9C2kF^Js!w7RNi@v&8($*;Tr$oy>cV7a8uN S&RAm^E*fY-f0K(g%KrfHwzif4 literal 0 HcmV?d00001 diff --git a/Compression/bin/HEncoder$Node.class b/Compression/bin/HEncoder$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..9fcce4c5042a2435e9f72adbb12ab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Fyp zJhtTiNU!J}nUC4y`i4ddh*M^V%SN*A{vey?O20Ao&FV{(#b-FNbx|HW!XE>axZ-Qm zx{NyXf`0qx0^Yazu}^D{ZR#c3V39z7nNbBh*yWDBtO!}6Klq92r&{d-SHthPc3itX w!`&GU=IhoBM_jL7gKtp(*OVf literal 0 HcmV?d00001 diff --git a/Compression/bin/genericheap.class b/Compression/bin/genericheap.class new file mode 100644 index 0000000000000000000000000000000000000000..16c2d03aa1029eb85be2e4cf7fee29187320d1b2 GIT binary patch literal 2858 zcma)8TT@e46#h2nyd zPQfX=nQh1BfoY&;$9`tdqNU?8PTyHz(gbGcl$_fl@9x?mub zcDD+4b+_yoIML2$acym3iH>HbS}YrgH2GX9=1X?j<~i-rwuaj6jzzcNR+kJ6#oGEU z#McdswzugR;$U7WW}Pnham<2+UIYGEd_9PM1AT4Sb)gT(LNG9BA%G594Os}G%fQi; zTX1ge<~N=4n!TAL_uf=7W9QcGvMYP<(5!B|6$3|7O?4*K$YI~TQq8}&*|A35thiq? z0imu6qBksz;go@(TS?|i)jef-D3%EeXK`-B|L*s2H^WHx8ZVITGCHfT;qKFlH! z-nVcCR}FN!m6Tmp{(;!)YFvfCX5l(k7_#!YT{55y4DHLf28>lMN?aUCSMAKxn|4Vl zDY$Q0xQ#o^vE@k3h^G}n827LiMA|@qqn{dGZs9(JL`L;JHJxZvwAQF`f-(f$4zs(} zZGLyE+k)vM3m@YX?kj?6BDUJ*a_bQ}lfw$R^vBwekdnk%NS3ecG>IN`}prb;s8B~-_ZBq z00S=(Nld=P@fS4saf07onEZzM?c_BJ{R|Z4Nkm`9Fh;mz7%%DQA=}BiO+Xkjv;0c> zt7;E)CVoa_AE%%5OlK4-K#$5|sdcB9MPfHjB(@>5&TpHD(cztn^S*;BX66Inhur@E511!JL9z^fVZh=#%?+XCvX2!^|FFzLBh( zj$oh=s~y2;lLCWYLSPU}c#n7+3W!ZBAhu=NeDbDv?7n79i8>bj7UqknnGNJFC8EOz zxcm*e)gqbTPN6VWFN~@NI>lZ%&6YdEt~kqn7^iK5dEyGyn2uRTHHKAuz>Hyb$PKQ1 z3ZGVS%)^%=3jPuY{>G3%%JEqWidcOBTai%$r;tpBgfM!CgPTL%@9Q z7r`~qh`M!-<(lHmP805V!ktwBXLZCnz;SFSfXspq6+kKS1D4XyyM1`bm8n2$YU3Vg z?X_I@TBer}VWa encoder = new HashMap<>(); + public HashMap decoder = new HashMap<>(); + + 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"); + + } + + public String compress(String str) { + String rv = ""; + for (int i = 0; i < str.length(); ++i) { + rv += encoder.get(str.charAt(i)); + } + return rv; + } + + 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..ce4b491a3c86 --- /dev/null +++ b/Compression/src/genericheap.java @@ -0,0 +1,93 @@ + + +import java.util.ArrayList; +import java.util.Comparator; + +public class genericheap { + + private ArrayList data = new ArrayList<>(); + private Comparator ctor; + + public genericheap(Comparator ctor) { + this.ctor=ctor; + } + + public int size() { + return data.size(); + } + + public boolean isEmpty() { + return data.isEmpty(); + } + + public void display() { + System.out.println(this.data); + } + + public void add(T integer) { + 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) { + 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); + } + } + +} From 09ddc5b70cf7c747accfd73565f5b7da14c0bf3c Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Tue, 3 Oct 2017 22:07:30 +0530 Subject: [PATCH 02/14] Create Any_to_Any Input : Source base , Source number , Destination base Output : Destination Number --- Conversions/AnytoAny | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Conversions/AnytoAny diff --git a/Conversions/AnytoAny b/Conversions/AnytoAny new file mode 100644 index 000000000000..2e43fb0e8829 --- /dev/null +++ b/Conversions/AnytoAny @@ -0,0 +1,28 @@ +import java.util.Scanner; + +//sn ,sb,db ---> ()dn +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); + } + +} From eade5eb1ca8820fe9c827105daca73db711365bd Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 23 Oct 2017 11:46:44 +0530 Subject: [PATCH 03/14] Rename AnytoAny to AnytoAny.java --- Conversions/{AnytoAny => AnytoAny.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Conversions/{AnytoAny => AnytoAny.java} (100%) diff --git a/Conversions/AnytoAny b/Conversions/AnytoAny.java similarity index 100% rename from Conversions/AnytoAny rename to Conversions/AnytoAny.java From 69f30600b9d7ef3203a9cf75e4ee5faa174ea6b4 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 23 Oct 2017 11:48:18 +0530 Subject: [PATCH 04/14] Update AnytoAny.java --- Conversions/AnytoAny.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Conversions/AnytoAny.java b/Conversions/AnytoAny.java index 2e43fb0e8829..a1acdb1a4863 100644 --- a/Conversions/AnytoAny.java +++ b/Conversions/AnytoAny.java @@ -1,6 +1,6 @@ import java.util.Scanner; - -//sn ,sb,db ---> ()dn +//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) { From e1721bb377b8403bf84e60ed9cc717ea7f8ae2b3 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 8 Jan 2018 19:16:32 +0530 Subject: [PATCH 05/14] added comments for some functions --- Compression/src/HEncoder.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Compression/src/HEncoder.java b/Compression/src/HEncoder.java index e0ebaa1d4fdb..37bd8fc0a0f0 100644 --- a/Compression/src/HEncoder.java +++ b/Compression/src/HEncoder.java @@ -4,8 +4,8 @@ public class HEncoder { - public HashMap encoder = new HashMap<>(); - public HashMap decoder = new HashMap<>(); + public HashMap encoder = new HashMap<>(); // in order to encode + public HashMap decoder = new HashMap<>(); // in order to decode private static class Node { @@ -81,6 +81,7 @@ private void traverse(Node node, String osf) { } + // compression work done here public String compress(String str) { String rv = ""; for (int i = 0; i < str.length(); ++i) { @@ -88,7 +89,9 @@ public String compress(String str) { } return rv; } + + //in order to decompress public String decompress(String str) { String s = ""; String code = ""; From 0e3e14ee98bd47390cc232781dd7428ae054a49b Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 8 Jan 2018 19:21:37 +0530 Subject: [PATCH 06/14] added comments --- Compression/src/genericheap.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Compression/src/genericheap.java b/Compression/src/genericheap.java index ce4b491a3c86..de00316f0d53 100644 --- a/Compression/src/genericheap.java +++ b/Compression/src/genericheap.java @@ -3,28 +3,28 @@ import java.util.ArrayList; import java.util.Comparator; -public class genericheap { +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) { + public genericheap(Comparator ctor) { // constructor to initialize the generic comparator this.ctor=ctor; } - public int size() { + public int size() { // returns the size of the arraylist data return data.size(); } - public boolean isEmpty() { + public boolean isEmpty() { // checks whether the list is empty or not :: return true or false for the same return data.isEmpty(); } - public void display() { + public void display() { //displays the list System.out.println(this.data); } - public void add(T integer) { + 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); } @@ -53,7 +53,7 @@ private boolean isLarger(int i, int j) { } } - private void swap(int ci, int pi) { + 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); From ba82daf6830640958fa54fe25accb886621983a3 Mon Sep 17 00:00:00 2001 From: Mahmoud khalil Date: Sun, 1 Apr 2018 20:50:41 +0300 Subject: [PATCH 07/14] Update Stacks.java --- Data Structures/Stacks/Stacks.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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; } From eca9da527e5f2d25543166643bec8d961ad79480 Mon Sep 17 00:00:00 2001 From: khalil2535 Date: Sun, 1 Apr 2018 22:09:06 +0300 Subject: [PATCH 08/14] rename AnytoAny to capital letters and adding package declaration --- Conversions/AnytoAny.java | 45 ++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Conversions/AnytoAny.java b/Conversions/AnytoAny.java index a1acdb1a4863..a86e2d042059 100644 --- a/Conversions/AnytoAny.java +++ b/Conversions/AnytoAny.java @@ -1,28 +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); - } +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); + } } From 122be4c2271c3a7d970a2660dce889c1be3ad204 Mon Sep 17 00:00:00 2001 From: kromzem Date: Wed, 25 Oct 2017 15:18:59 +0200 Subject: [PATCH 09/14] added "Perlin-Noise-Algorithm" --- Others/PerlinNoise.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 Others/PerlinNoise.java diff --git a/Others/PerlinNoise.java b/Others/PerlinNoise.java new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/Others/PerlinNoise.java @@ -0,0 +1 @@ + From 9d1785163a0ee1d5c2f3d19e8e628132f29c7d0a Mon Sep 17 00:00:00 2001 From: kromzem Date: Wed, 25 Oct 2017 15:22:16 +0200 Subject: [PATCH 10/14] added "Perlin-Noise-Algorithm" --- Others/PerlinNoise.java | 129 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/Others/PerlinNoise.java b/Others/PerlinNoise.java index 8b137891791f..d8e04a85776e 100644 --- a/Others/PerlinNoise.java +++ b/Others/PerlinNoise.java @@ -1 +1,130 @@ +import java.util.Random; +import java.util.Scanner; +public class PerlinNoise { + 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); + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + base[x][y] = random.nextFloat(); + } + } + + //calculate octaves + for(int octave = 0; octave < octaveCount; octave++) { + noiseLayers[octave] = generatePerlinNoiseLayer(base, width, height, octave); + } + + float amplitude = 1f; + float totalAmplitude = 0f; + + //calculate perlin noise + 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++) { + perlinNoise[x][y] += noiseLayers[octave][x][y] * amplitude; + } + } + } + + //normalize + for(int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + perlinNoise[x][y] /= totalAmplitude; + } + } + + return perlinNoise; + } + + static float[][] generatePerlinNoiseLayer(float[][] base, int width, int height, int octave) { + float[][] perlinNoiseLayer = new float[width][height]; + + int period = 1 << octave; + float frequency = 1f / period; + + for(int x = 0; x < width; x++) { + int x0 = (x / period) * period; + int x1 = (x0 + period) % width; + float horizintalBlend = (x - x0) * frequency; + + for(int y = 0; y < height; y++) { + int y0 = (y / period) * period; + int y1 = (y0 + period) % height; + float verticalBlend = (y - y0) * frequency; + + float top = interpolate(base[x0][y0], base[x1][y0], horizintalBlend); + float bottom = interpolate(base[x0][y1], base[x1][y1], horizintalBlend); + + perlinNoiseLayer[x][y] = interpolate(top, bottom, verticalBlend); + } + } + + return perlinNoiseLayer; + } + + 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(); + } + } +} From 5d164eac3649e79047a835f091132f7c4c87deac Mon Sep 17 00:00:00 2001 From: Kromzem Date: Fri, 27 Oct 2017 12:28:54 +0200 Subject: [PATCH 11/14] added some more comments --- Others/PerlinNoise.java | 44 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/Others/PerlinNoise.java b/Others/PerlinNoise.java index d8e04a85776e..1d0f795a3a35 100644 --- a/Others/PerlinNoise.java +++ b/Others/PerlinNoise.java @@ -1,20 +1,32 @@ 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 + //calculate octaves with different roughness for(int octave = 0; octave < octaveCount; octave++) { noiseLayers[octave] = generatePerlinNoiseLayer(base, width, height, octave); } @@ -22,19 +34,21 @@ static float[][] generatePerlinNoise(int width, int height, int octaveCount, flo float amplitude = 1f; float totalAmplitude = 0f; - //calculate perlin noise + //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 + //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; @@ -44,25 +58,39 @@ static float[][] generatePerlinNoise(int width, int height, int octaveCount, flo 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]; - int period = 1 << octave; - float frequency = 1f / period; + //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); } } @@ -70,6 +98,12 @@ static float[][] generatePerlinNoiseLayer(float[][] base, int width, int height, 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; } From e080b2622bc44e592b26d37392e167be9bb1e0b4 Mon Sep 17 00:00:00 2001 From: PARISOT paul andrea Date: Tue, 3 Apr 2018 20:00:10 +0200 Subject: [PATCH 12/14] adding of the combSort.java modification of CombSort --- Sorts/CombSort.java | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Sorts/CombSort.java diff --git a/Sorts/CombSort.java b/Sorts/CombSort.java new file mode 100644 index 000000000000..05fc3a8b1930 --- /dev/null +++ b/Sorts/CombSort.java @@ -0,0 +1,68 @@ +/** + * + * @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 1 4 6 9 12 23 54 78 231 + for(int i=0; i a b c d e + for(int i=0; i Date: Thu, 5 Apr 2018 20:20:56 +0200 Subject: [PATCH 13/14] modification of CombSort --- Sorts/CombSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/CombSort.java b/Sorts/CombSort.java index 05fc3a8b1930..078cb4fea343 100644 --- a/Sorts/CombSort.java +++ b/Sorts/CombSort.java @@ -48,7 +48,7 @@ public static void main(String[] args) { cS(array); - // Output => 1 4 6 9 12 23 54 78 231 + // Output => 1 4 6 9 12 23 54 78 231 for(int i=0; i Date: Sun, 8 Apr 2018 17:10:30 +0200 Subject: [PATCH 14/14] Adding of Sorts/OddEven.java, correction of CombSort.java --- Sorts/CombSort.java | 103 ++++++++++++++++++++++---------------------- Sorts/OddEven.java | 62 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 52 deletions(-) create mode 100644 Sorts/OddEven.java diff --git a/Sorts/CombSort.java b/Sorts/CombSort.java index 078cb4fea343..0fc1fe781d9e 100644 --- a/Sorts/CombSort.java +++ b/Sorts/CombSort.java @@ -1,68 +1,67 @@ /** - * - * @author Paul Parisot (https://github.com/tosirap) - * - */ +* +* @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 - **/ + /** + * 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; + 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; - } + while(intervalle > 1 || echange){ + intervalle /= 1.3; + if(intervalle < 1){ + intervalle = 1; + } - int i=0; - echange= false; + 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++; - } - } - } + 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) { + public static void main(String[] args) { - // Integer Input - int[] arr1 = {4,23,6,78,1,54,231,9,12}; + // Integer Input + int[] arr1 = {4,23,6,78,1,54,231,9,12}; - Integer[] array = new Integer[arr1.length]; - for (int i=0;i 1 4 6 9 12 23 54 78 231 - for(int i=0; i 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(); - System.out.println(); + // String Input + String[] array1 = {"c", "a", "e", "b","d"}; - // String Input - String[] array1 = {"c", "a", "e", "b","d"}; + cS(array1); - cS(array1); - - //Output => a b c d e - for(int i=0; i 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy