Merge remote-tracking branch 'upstream/master'

This commit is contained in:
sahilb2 2018-01-02 18:51:27 -08:00
commit d6a5572aa1
23 changed files with 128 additions and 121 deletions

View File

@ -3,7 +3,7 @@ import java.util.*;
import java.util.Scanner;
import javax.swing.*;
public class HexaToBin {
public class HexaDecimalToBinary {
private final int LONG_BITS = 8;
@ -28,7 +28,7 @@ public class HexaToBin {
//Testing Numbers:
String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB",
"19", "01", "02", "03", "04"};
Convert objConvert = new Convert();
HexaDecimalToBinary objConvert = new HexaDecimalToBinary();
for (String num : hexNums) {
objConvert.convert(num);

View File

@ -11,38 +11,37 @@ public class OctalToDecimal {
/**
* Main method
*
* @param args Command line arguments
* @param args
* Command line arguments
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int o = sc.nextInt();
System.out.println("Decimal equivalent: " + convertOctalToDecimal(o));
System.out.print("Octal Input: ");
String inputOctal = sc.nextLine();
int result = convertOctalToDecimal(inputOctal);
if (result != -1)
System.out.println("Result convertOctalToDecimal : " + result);
sc.close();
}
/**
* This method converts an octal number to
* a decimal number.
* This method converts an octal number to a decimal number.
*
* @param o The octal number
* @param inputOctal
* The octal number
* @return The decimal number
*/
public static int convertOctalToDecimal(int o) {
System.out.print("Octal Input: ");
// Read the input from the console which we are expecting as an octal number:
Scanner s = new Scanner(System.in);
String inputHex = s.nextLine();
try{
public static int convertOctalToDecimal(String inputOctal) {
try {
// Actual conversion of Octal to Decimal:
Integer outputDecimal = Integer.parseInt(inputHex, 8);
System.out.println("Decimal Equivalent : " + outputDecimal);
}
catch(NumberFormatException ne){
// Printing a warning message if the input is not a valid octal number:
Integer outputDecimal = Integer.parseInt(inputOctal, 8);
return outputDecimal;
} catch (NumberFormatException ne) {
// Printing a warning message if the input is not a valid octal
// number:
System.out.println("Invalid Input, Expecting octal number 0-7");
}
finally{
s.close();
return -1;
}
}
}

View File

@ -6,7 +6,7 @@ import java.util.*;
* @author Unknown
*
*/
public class bfs{
public class BFS{
/**
* The BFS implemented in code to use.

View File

@ -7,7 +7,7 @@ import java.util.*;
*
*/
public class dfs{
public class DFS{
/**
* Implementation in code of a DFS

View File

@ -120,7 +120,7 @@ class AdjacencyMatrixGraph {
}
/**
* this gives a list of verticies in the graph and their adjacencies
* this gives a list of vertices in the graph and their adjacencies
*
* @return returns a string describing this graph
*/

View File

@ -100,7 +100,7 @@ class PrimMST
| / \ |
(3)-------(4)
9 */
MST t = new MST();
PrimMST t = new PrimMST();
int graph[][] = new int[][] {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},

View File

@ -1,4 +1,4 @@
public class AVLtree {
public class AVLTree {
private Node root;
@ -200,7 +200,7 @@ public class AVLtree {
}
public static void main(String[] args) {
AVLtree tree = new AVLtree();
AVLTree tree = new AVLTree();
System.out.println("Inserting values 1 to 10");
for (int i = 1; i < 10; i++)

View File

@ -9,12 +9,12 @@ class Node
}
}
class BinaryTree
public class LevelOrderTraversal
{
// Root of the Binary Tree
Node root;
public BinaryTree()
public LevelOrderTraversal()
{
root = null;
}
@ -65,7 +65,7 @@ class BinaryTree
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
LevelOrderTraversal tree = new LevelOrderTraversal();
tree.root= new Node(1);
tree.root.left= new Node(2);
tree.root.right= new Node(3);

View File

@ -14,7 +14,7 @@ class Node {
}
/* Class to print Level Order Traversal */
class BinaryTree {
public class LevelOrderTraversalQueue {
Node root;
@ -49,7 +49,7 @@ class BinaryTree {
{
/* creating a binary tree and entering
the nodes */
BinaryTree tree_level = new BinaryTree();
LevelOrderTraversalQueue tree_level = new LevelOrderTraversalQueue();
tree_level.root = new Node(1);
tree_level.root.left = new Node(2);
tree_level.root.right = new Node(3);

View File

@ -78,7 +78,7 @@ class Tree
}
// Driver class to test above methods
public class Main
public class PrintTopViewofTree
{
public static void main(String[] args)
{

View File

@ -10,7 +10,7 @@ class Node
}
}
public class BinaryTree
public class ValidBSTOrNot
{
//Root of the Binary Tree
Node root;
@ -47,7 +47,7 @@ public class BinaryTree
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
ValidBSTOrNot tree = new ValidBSTOrNot();
tree.root = new Node(4);
tree.root.left = new Node(2);
tree.root.right = new Node(5);

View File

@ -6,7 +6,7 @@
*
*/
public class Levenshtein_distance{
public class LevenshteinDistance{
private static int minimum(int a, int b, int c){
if(a < b && a < c){
return a;

View File

@ -1,4 +1,4 @@
public class HeapSort
public class heap_sort
{
public void sort(int arr[])
{
@ -64,7 +64,7 @@ public class HeapSort
int arr[] = {12, 11, 13, 5, 6, 7};
int n = arr.length;
HeapSort ob = new HeapSort();
heap_sort ob = new heap_sort();
ob.sort(arr);
System.out.println("Sorted array is");

View File

@ -0,0 +1,55 @@
import java.util.Scanner;
/**
*
* @author Nishita Aggarwal
*
* Brian Kernighans Algorithm
*
* algorithm to count the number of set bits in a given number
*
* Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the
* rightmost set bit).
* So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit.
*
* If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count.
*
*
* Time Complexity: O(logn)
*
*/
public class BrianKernighanAlgorithm {
/**
* @param num: number in which we count the set bits
*
* @return int: Number of set bits
* */
static int countSetBits(int num)
{
int cnt = 0;
while(num != 0)
{
num = num & (num-1);
cnt++;
}
return cnt;
}
/**
*
* @param args : command line arguments
*
*/
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int setBitCount = countSetBits(num);
System.out.println(setBitCount);
sc.close();
}
}

View File

@ -9,7 +9,7 @@ import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public class Dijkshtra {
public static void main(String[] args) throws IOException {
Scanner in =new Scanner(System.in);

View File

@ -9,7 +9,7 @@ public class FibToN {
// print fibonacci sequence less than N
int first = 0, second = 1;
//first fibo and second fibonacci are 0 and 1 respectively
scn.close();
while(first <= N){
//print first fibo 0 then add second fibo into it while updating second as well

View File

@ -1,45 +0,0 @@
/**
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
* up to a given value.
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
* (This illustration is also in the github repository)
*
* @author Unknown
*
*/
public class FindingPrimes{
/**
* The Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
SOE(20); //Example: Finds all the primes up to 20
}
/**
* The method implementing the Sieve of Eratosthenes
*
* @param n Number to perform SOE on
*/
public static void SOE(int n){
boolean sieve[] = new boolean[n];
int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n
sieve[0] = false;
sieve[1] = false;
for(int i = 2; i < n; i++)
sieve[i] = true; //Set every index to true except index 0 and 1
for(int i = 2; i< check; i++){
if(sieve[i]==true) //If i is a prime
for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime)
sieve[j] = false; //Set every multiple of i to false
}
for(int i = 0; i< n; i++){
if(sieve[i]==true)
System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19
}
}
}

View File

@ -6,7 +6,7 @@ class FloydTriangle {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
int r = sc.nextInt(), n = 0;
sc.close();
for(int i=0; i < r; i++) {
for(int j=0; j <= i; j++) {
System.out.print(++n + " ");

View File

@ -1,5 +1,5 @@
import java.util.*;
public class Array {
public class InsertDeleteInArray {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); // Input statement

View File

@ -4,7 +4,7 @@ import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public class RootPrecision {
public static void main(String[] args) {
//take input

View File

@ -1,6 +1,6 @@
import java.util.*;
public class Postfix {
public class StackPostfixNotation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"

View File

@ -7,7 +7,7 @@ import java.util.Scanner;
* @author Marcus
*
*/
class CountTheWords{
public class countwords{
public static void main(String[] args){
Scanner input = new Scanner(System.in);

View File

@ -1,30 +1,28 @@
import java.util.Scanner;
class krishnamurthy
{
int fact(int n)
{
int i,p=1;
for(i=n;i>=1;i--)
p=p*i;
class krishnamurthy {
static int fact(int n) {
int i, p = 1;
for (i = n; i >= 1; i--)
p = p * i;
return p;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,s=0;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b, s = 0;
System.out.print("Enter the number : ");
a=sc.nextInt();
int n=a;
while(a>0)
{
b=a%10;
s=s+fact(b);
a=a/10;
a = sc.nextInt();
int n = a;
while (a > 0) {
b = a % 10;
s = s + fact(b);
a = a / 10;
}
if(s==n)
System.out.print(n+" is a krishnamurthy number");
if (s == n)
System.out.print(n + " is a krishnamurthy number");
else
System.out.print(n+" is not a krishnamurthy number");
System.out.print(n + " is not a krishnamurthy number");
sc.close();
}
}