Skip to content

adding of the combSort.java #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Binary file added Compression/bin/HEncoder$Node$Nodecomparator.class
Binary file not shown.
Binary file added Compression/bin/HEncoder$Node.class
Binary file not shown.
Binary file added Compression/bin/HEncoder.class
Binary file not shown.
Binary file added Compression/bin/compressclient.class
Binary file not shown.
Binary file added Compression/bin/genericheap.class
Binary file not shown.
108 changes: 108 additions & 0 deletions Compression/src/HEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;

public class HEncoder {

public HashMap<Character, String> encoder = new HashMap<>(); // in order to encode
public HashMap<String, Character> 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<Node> {

@Override
public int compare(Node o1, Node o2) {
return o2.freq - o1.freq;
}

}
}

public HEncoder(String feeder) {
// 1. freq map
HashMap<Character, Integer> 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<Node> heap = new genericheap<Node>(Node.Ctor);
ArrayList<Character> 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;
}
}
11 changes: 11 additions & 0 deletions Compression/src/compressclient.java
Original file line number Diff line number Diff line change
@@ -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"));
}

}
93 changes: 93 additions & 0 deletions Compression/src/genericheap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@


import java.util.ArrayList;
import java.util.Comparator;

public class genericheap<T> { // create a generic heap class <T> , where T can be of any type.

private ArrayList<T> data = new ArrayList<>();
private Comparator<T> ctor;

public genericheap(Comparator<T> 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 <t> 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);
}
}

}
29 changes: 29 additions & 0 deletions Conversions/AnytoAny.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
8 changes: 6 additions & 2 deletions Data Structures/Stacks/Stacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void push(int value){
stackArray[top] = value;
}else{
resize(maxSize*2);
push(value);// don't forget push after resizing
}
}

Expand All @@ -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");
Expand All @@ -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;
}
Expand Down
Loading