Loading...
More Java Posts
import java.util.*;public class HeapSortMain {public static void buildheap(int []arr) {/** As last non leaf node will be at (arr.length-1)/2* so we will start from this location for heapifying the elements* */for(int i=(arr.length-1)/2; i>=0; i--){heapify(arr,i,arr.length-1);}}public static void heapify(int[] arr, int i,int size) {int left = 2*i+1;int right = 2*i+2;int max;if(left <= size && arr[left] > arr[i]){max=left;} else {max=i;}if(right <= size && arr[right] > arr[max]) {max=right;}// If max is not current node, exchange it with max of left and right childif(max!=i) {exchange(arr,i, max);heapify(arr, max,size);}}public static void exchange(int[] arr,int i, int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;}public static int[] heapSort(int[] arr) {buildheap(arr);int sizeOfHeap=arr.length-1;for(int i=sizeOfHeap; i>0; i--) {exchange(arr,0, i);sizeOfHeap=sizeOfHeap-1;heapify(arr, 0,sizeOfHeap);}return arr;}public static void main(String[] args) {int[] arr={1,10,16,19,3,5};System.out.println("Before Heap Sort : ");System.out.println(Arrays.toString(arr));arr=heapSort(arr);System.out.println("=====================");System.out.println("After Heap Sort : ");System.out.println(Arrays.toString(arr));}}
//sample code to write 100 random ints to a file, 1 per lineimport java.io.PrintStream;import java.io.IOException;import java.io.File;import java.util.Random;public class WriteToFile {public static void main(String[] args) {try {PrintStream writer = new PrintStream(new File("randInts.txt"));Random r = new Random();final int LIMIT = 100;for (int i = 0; i < LIMIT; i++) {writer.println(r.nextInt());}writer.close();} catch (IOException e) {System.out.println("An error occured while trying to write to the file");}}}
import java.io.File;import java.io.IOException;import java.util.Map;import java.util.Scanner;import java.util.TreeMap;public class SimpleWordCounter {public static void main(String[] args) {try {File f = new File("ciaFactBook2008.txt");Scanner sc;sc = new Scanner(f);// sc.useDelimiter("[^a-zA-Z']+");Map<String, Integer> wordCount = new TreeMap<String, Integer>();while(sc.hasNext()) {String word = sc.next();if(!wordCount.containsKey(word))wordCount.put(word, 1);elsewordCount.put(word, wordCount.get(word) + 1);}// show resultsfor(String word : wordCount.keySet())System.out.println(word + " " + wordCount.get(word));System.out.println(wordCount.size());}catch(IOException e) {System.out.println("Unable to read from file.");}}}
public static String addBinary(){// The two input Strings, containing the binary representation of the two values:String input0 = "1010";String input1 = "10";// Use as radix 2 because it's binaryint number0 = Integer.parseInt(input0, 2);int number1 = Integer.parseInt(input1, 2);int sum = number0 + number1;return Integer.toBinaryString(sum); //returns the answer as a binary value;}
import java.util.ArrayList;import java.util.Iterator;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);char[] variables = setVariables(in);int[] minTerms = setMinTerms(in);String[] binaryMinTerms = decimalToBinary(variables, minTerms);ArrayList<Group> groups = computeSuccessiveGroups(minTerms, binaryMinTerms);displayInput(variables, minTerms);printPIs(groups, variables);}private static char[] setVariables(Scanner in) {System.out.print("Enter the number of variables: ");int numVariables = in.nextInt();char[] variables = new char[numVariables];System.out.print("Enter each variable separated by spaces: ");for(int i = 0; i < numVariables; i++) {variables[i] = in.next().charAt(0);}return variables;}private static int[] setMinTerms(Scanner in) {System.out.print("Enter the number of min terms: ");int numMinTerms = in.nextInt();int[] minTerms = new int[numMinTerms];System.out.print("Enter each min term separated by spaces: ");for(int i = 0; i < numMinTerms; i++) {minTerms[i] = in.nextInt();}return minTerms;}private static String[] decimalToBinary(char[] variables, int[] minTerms) {String[] binaryMinTerms = new String[minTerms.length];for(int i = 0; i < minTerms.length; i++) {binaryMinTerms[i] = Integer.toBinaryString(minTerms[i]);if(binaryMinTerms[i].length() < variables.length) {String zeros = "";int len = binaryMinTerms[i].length();while(len < variables.length) {zeros = zeros.concat("0");len++;}binaryMinTerms[i] = zeros + binaryMinTerms[i];}}return binaryMinTerms;}private static void displayInput(char[] variables, int[] minTerms) {System.out.print("Function: f(");for(int i = 0; i < variables.length; i++) {if(i == variables.length-1)System.out.print(variables[i]);elseSystem.out.print(variables[i] + ", ");}System.out.print(") = SIGMAm(");for(int i = 0; i < minTerms.length; i++) {if(i == minTerms.length-1)System.out.print(minTerms[i]);elseSystem.out.print(minTerms[i] + ", ");}System.out.println(")");}private static ArrayList<Group> computeSuccessiveGroups(int[] minTerms, String[] binaryMinTerms) {ArrayList<Group> groups = new ArrayList<Group>();for(int i = 0; i < minTerms.length; i++)groups.add(new Group(Integer.toString(minTerms[i]), binaryMinTerms[i]));int sizeBeforeRemoval = 0, currentSize;while(sizeBeforeRemoval != groups.size()) {currentSize = groups.size();for(int i = 0; i < currentSize; i++) {for(int j = i+1; j < currentSize; j++) {if(differences(groups.get(i).binary, groups.get(j).binary) == 1) {String combinedBin = combineBinary(groups.get(i).binary, groups.get(j).binary);groups.get(i).grouped = true;groups.get(j).grouped = true;if(isUniqueBin(groups, combinedBin)) {String newTuple = groups.get(i).decimal + "," + groups.get(j).decimal;groups.add(new Group(newTuple, combinedBin));}else break;}}}sizeBeforeRemoval = groups.size();Iterator<Group> iter = groups.iterator();while(iter.hasNext()) {Group next = iter.next();if(next.grouped)iter.remove();}}return groups;}private static int differences(String a, String b) {int diff = 0;for(int i = 0; i < a.length(); i++)if(a.charAt(i) != b.charAt(i))diff++;return diff;}private static String combineBinary(String a, String b) {String combined = "";for(int i = 0; i < a.length(); i++) {if(a.charAt(i) != b.charAt(i))combined = combined.concat("-");elsecombined = combined.concat(Character.toString(a.charAt(i)));}return combined;}private static boolean isUniqueBin(ArrayList<Group> groups, String s) {for(int i = 0; i < groups.size(); i++)if(groups.get(i).binary.equals(s))return false;return true;}private static void printPIs(ArrayList<Group> groups, char[] variables) {System.out.println("Prime Implicants: ");for(int i = 0; i < groups.size(); i++)System.out.println(groups.get(i).decimal + " | " + binaryToMinTerm(variables, groups.get(i).binary));}private static String binaryToMinTerm(char[] variables, String s) {String minTerm = "";for(int i = 0; i < s.length(); i++) {if(s.charAt(i) != '-') {if(s.charAt(i) == '0')minTerm = minTerm.concat(Character.toString(variables[i]) + "'");elseminTerm = minTerm.concat(Character.toString(variables[i]));}}return minTerm;}}
import java.util.Arrays;public class QuickSortMain {private static int array[];public static void sort(int[] arr) {if (arr == null || arr.length == 0) {return;}array = arr;quickSort(0, array.length-1);}private static void quickSort(int left, int right) {int i = left;int j = right;// find pivot number, we will take it as midint pivot = array[left+(right-left)/2];while (i <= j) {/*** In each iteration, we will increment left until we find element greater than pivot* We will decrement right until we find element less than pivot*/while (array[i] < pivot) { i++; } while (array[j] > pivot) {j--;}if (i <= j) {exchange(i, j);//move index to next position on both sidesi++;j--;}}// call quickSort() method recursivelyif (left < j)quickSort(left, j);if (i < right)quickSort(i, right);}private static void exchange(int i, int j) {int temp = array[i];array[i] = array[j];array[j] = temp;}public static void main(String a[]){int[] input = {33,21,45,64,55,34,11,8,3,5,1};System.out.println("Before Sorting : ");System.out.println(Arrays.toString(input));sort(input);System.out.println("==================");System.out.println("After Sorting : ");System.out.println(Arrays.toString(array));}}