You are on page 1of 2

public static Stack<Integer>[] tower = new Stack[4]; public static String printPostFix(String str){ public <T extends Comparable>

extends Comparable> void bubbleSort(T[] data, int min, int max){


Stack stack = new Stack();
public static int N; String postfix = ""; for(int i = min ; i < max ; i++ ){
for(int i=0;i<str.length();i++){ for (int j = min ; j < max ; j++){
public static void main(String[] args) { char c = str.charAt(i);
Scanner scan = new Scanner(System.in); if(Character.isLetter(c)){ if(data[i].compareTo(data[j])< 0){
postfix = postfix + c;}
tower[1] = new Stack<Integer>(); else if(c == '('){ T keeper = data[i];
tower[2] = new Stack<Integer>(); continue;} data[i] = data[j];
else if(c == ')'){
tower[3] = new Stack<Integer>(); postfix = postfix + ((Character)stack.pop()).toString();} data[j] = keeper;}}}}
else{stack.push(c);}} public <T extends Comparable> boolean binarySearch(T[] data, int min, int max, T target) {
System.out.println("number of weights ?"); return postfix;}
int num = scan.nextInt(); public static String printPreFix(String str){ bubbleSort(data,min,max);
Stack stack = new Stack();
System.out.println("\n"); String prefix = ""; boolean found = false;
N = num; for(int i=str.length()-1;i>=0;i--){ int midpoint = (min + max) / 2;
char c = str.charAt(i);
toh(num);} if(Character.isLetter(c)){ if (data[midpoint].compareTo(target) == 0) {found = true;
prefix = ((Character)c).toString() + prefix;}
public static void toh(int n) { else if(c == '('){ } else if (data[midpoint].compareTo(target) > 0) {
for (int d = n; d > 0; d--) { prefix = ((Character)stack.pop()).toString() + prefix;} if (min <= midpoint - 1) {
else if(c == ')'){continue;}else{
tower[1].push(d);} stack.push(c);}} found = binarySearch(data, min, midpoint - 1, target);}
display(); return prefix;} } else if (midpoint + 1 <= max) {
public static void main(String[] args) {
move(n, 1, 2, 3);} String infix = "(a+(a*b))"; found = binarySearch(data, midpoint + 1, max, target);}
System.out.println("Postfix : " + printPostFix(infix));
public static void move(int n, int a, int b, int c) { System.out.println("Prefix : " + printPreFix(infix));} return found;}
if (n > 0) {
move(n - 1, a, c, b); public class PostfixToInfix { public void sort( int[] a)
private boolean isOperator(char c){
int d = tower[a].pop(); if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^') {
tower[c].push(d); return true;
display(); return false;} int i, m = a[0], exp = 1, n = a.length;
public String convert(String postfix){
move(n - 1, b, a, c);}} Stack<String> s = new Stack<>(); int[] b = new int[10];
public static void display() { for(int i = 0; i < postfix.length(); i++){ for (i = 1; i < n; i++)
for (int i = N - 1; i >= 0; i--) { char c = postfix.charAt(i);
String d1 = " ", d2 = " ", d3 = " ";
if(isOperator(c)){ {
String b = s.pop();
try { String a = s.pop(); if (a[i] > m)
s.push("("+a+c+b+")");}
d1 = String.valueOf(tower[1].get(i)); else {
} catch (Exception e) {} s.push(""+c);} m = a[i];
try { return s.pop(); }}
d2 = String.valueOf(tower[2].get(i)); public static void main(String[] args) { }
} catch (Exception e) {} PostfixToInfix obj = new PostfixToInfix(); }
try { Scanner sc =new Scanner(System.in);
System.out.print("Postfix : ");
d3 = String.valueOf(tower[3].get(i));
} catch (Exception e) {}
String postfix = sc.next(); while (m / exp > 0)
System.out.println("Infix : "+obj.convert(postfix)); }
System.out.println(" " + d1 + " | " + d2 + " | " + d3);} {
System.out.println("---------------"); int[] bucket = new int[10];
System.out.println(" A | B | C"); for (i = 0; i < n; i++)
System.out.println("\n");}
{
bucket[(a[i] / exp) % 10]++;
public static void main(String[] args) { }
Scanner a = new Scanner (System.in);
int num = a.nextInt();
doTowers(num, 'A', 'B', 'C'); for (i = 1; i < 10; i++)
} {
public static void doTowers(int topN, char from,
char inter, char to) { bucket[i] += bucket[i - 1];
if (topN == 1){ }
System.out.println("Disk 1 from "
for (i = n - 1; i >= 0; i--)
+ from + "da to " + to); {
}else { b[--bucket[(a[i] / exp) % 10]] = a[i];
doTowers(topN - 1, from, to, inter);
System.out.println("Disk " }
+ topN + "s from " + from + " to " + to); for (i = 0; i < n; i++)
doTowers(topN - 1, inter, from, to);}}
{
a[i] = b[i];
}
exp *= 10;}}
public class MySorting {
public <T extends Comparable> void bubbleSort(T[] data, int min, int max) {
for (int i = min; i < max; i++) {
for (int j = min; j < max; j++) {
if (data[i].compareTo(data[j]) < 0) {
T keeper = data[i];
data[i] = data[j];
data[j] = keeper;
}
}
}
}public static <T extends Comparable<? super T>> void selectionSort(T[] data) {
int min;
T temp;
for (int index = 0; index < data.length - 1; index++) {
min = index;
for (int scan = index + 1; scan < data.length; scan++) {
if (data[scan].compareTo(data[min]) < 0) {
min = scan;
}
}
temp = data[min];
data[min] = data[index];
data[index] = temp;}}
public static <T extends Comparable<? super T>> void insertionSort(T[] data) {
for (int index = 1; index < data.length; index++) {
T key = data[index];
int position = index;
while (position > 0 && data[position - 1].compareTo(key) > 0) {
data[position] = data[position - 1];
position--;}
data[position] = key;}}

public static <T extends Comparable<? super T>> void mergeSort(T[] data, int min, int max) {
T[] temp;
int index1, left, right;
if (min == max) {
return;
}
int size = max - min + 1;
int pivot = (min + max) / 2;
temp = (T[]) (new Comparable[size]);
mergeSort(data, min, pivot); // sort left half of list
mergeSort(data, pivot + 1, max); // sort right half of list
for (index1 = 0; index1 < size; index1++) {
temp[index1] = data[min + index1];
}
left = 0;
right = pivot - min + 1;
for (index1 = 0; index1 < size; index1++) {
if (right <= max - min) {
if (left <= pivot - min) {
if (temp[left].compareTo(temp[right]) > 0) {
data[index1 + min] = temp[right++];
} else { implements --- extends --- IOException e
data[index1 + min] = temp[left++];
}
} else {
data[index1 + min] = temp[right++];
}
} else {
data[index1 + min] = temp[left++];}}

}}

You might also like