Skip to content

Commit 75c55b8

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
New Merges
2 parents 73bb72b + b21444d commit 75c55b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+5375
-171
lines changed

.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry excluding="data_structures/" kind="src" path=""/>
4+
<classpathentry kind="src" path="data_structures"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

Binary Search.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

Bubble Sort.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

Conversions/AnyBaseToDecimal.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
4+
/**
5+
*
6+
* @author Varun Upadhyay (https://github.com/varunu28)
7+
*
8+
*/
9+
10+
// Driver program
11+
public class AnyBaseToDecimal {
12+
public static void main (String[] args) throws Exception{
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
String inp = br.readLine();
16+
int base = Integer.parseInt(br.readLine());
17+
18+
System.out.println("Input in base " + base + " is: " + inp);
19+
System.out.println("Decimal value of " + inp + " is: " + convertToDecimal(inp, base));
20+
21+
br.close();
22+
}
23+
24+
/**
25+
* This method produces a decimal value of any given input number of any base
26+
* @param inp_num String of which we need the decimal value and base in integer format
27+
* @return string format of the decimal value
28+
*/
29+
30+
public static String convertToDecimal(String inp_num, int base) {
31+
int len = inp_num.length();
32+
int num = 0;
33+
int pow = 1;
34+
35+
for (int i=len-1; i>=0; i--) {
36+
if (valOfChar(inp_num.charAt(i)) >= base) {
37+
return "Invalid Number";
38+
}
39+
num += valOfChar(inp_num.charAt(i))*pow;
40+
pow *= base;
41+
}
42+
return String.valueOf(num);
43+
}
44+
45+
/**
46+
* This method produces integer value of the input character and returns it
47+
* @param c Char of which we need the integer value of
48+
* @return integer value of input char
49+
*/
50+
51+
public static int valOfChar(char c) {
52+
if (c >= '0' && c <= '9') {
53+
return (int)c - '0';
54+
}
55+
else {
56+
return (int)c - 'A' + 10;
57+
}
58+
}
59+
}

Conversions/BinaryToDecimal.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* This class converts a Binary number to a Decimal number
5+
*
6+
* @author Unknown
7+
*
8+
*/
9+
class BinaryToDecimal
10+
{
11+
12+
/**
13+
* Main Method
14+
*
15+
* @param args Command line arguments
16+
*/
17+
public static void main(String args[])
18+
{
19+
Scanner sc=new Scanner(System.in);
20+
int n,k,d,s=0,c=0;
21+
System.out.print("Binary number: ");
22+
n=sc.nextInt();
23+
k=n;
24+
while(k!=0)
25+
{
26+
d=k%10;
27+
s+=d*(int)Math.pow(2,c++);
28+
k/=10;
29+
}
30+
System.out.println("Decimal equivalent:"+s);
31+
sc.close();
32+
}
33+
}

Conversions/BinaryToOctal.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Converts any Binary number to an Octal Number
5+
*
6+
* @author Zachary Jones
7+
*
8+
*/
9+
public class BinaryToOctal {
10+
11+
/**
12+
* Main method
13+
*
14+
* @param args Command line arguments
15+
*/
16+
public static void main(String args[]) {
17+
Scanner sc = new Scanner(System.in);
18+
int b = sc.nextInt();
19+
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
20+
sc.close();
21+
22+
}
23+
24+
/**
25+
* This method converts a binary number to
26+
* an octal number.
27+
*
28+
* @param b The binary number
29+
* @return The octal number
30+
*/
31+
public static int convertBinaryToOctal(int b) {
32+
int o = 0, r=0, j =1 ;
33+
while(b!=0)
34+
{
35+
r = b % 10;
36+
o = o + r * j;
37+
j = j * 2;
38+
b = b / 10;
39+
}
40+
return o;
41+
}
42+
43+
}

Conversions/DecimalToAnyBase.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayList;
4+
5+
/**
6+
*
7+
* @author Varun Upadhyay (https://github.com/varunu28)
8+
*
9+
*/
10+
11+
// Driver Program
12+
public class DecimalToAnyBase {
13+
public static void main (String[] args) throws Exception{
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
System.out.println("Enter the decimal input below: ");
16+
int decInput = Integer.parseInt(br.readLine());
17+
System.out.println();
18+
19+
System.out.println("Enter the base below: ");
20+
int base = Integer.parseInt(br.readLine());
21+
System.out.println();
22+
23+
System.out.println("Decimal Input" + " is: " + decInput);
24+
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
25+
26+
br.close();
27+
}
28+
29+
/**
30+
* This method produces a String value of any given input decimal in any base
31+
* @param inp Decimal of which we need the value in base in String format
32+
* @return string format of the converted value in the given base
33+
*/
34+
35+
public static String convertToAnyBase(int inp, int base) {
36+
ArrayList<Character> charArr = new ArrayList<>();
37+
38+
while (inp > 0) {
39+
charArr.add(reVal(inp%base));
40+
inp /= base;
41+
}
42+
43+
StringBuilder str = new StringBuilder(charArr.size());
44+
45+
for(Character ch: charArr)
46+
{
47+
str.append(ch);
48+
}
49+
50+
return str.reverse().toString();
51+
}
52+
53+
/**
54+
* This method produces character value of the input integer and returns it
55+
* @param num integer of which we need the character value of
56+
* @return character value of input integer
57+
*/
58+
59+
public static char reVal(int num) {
60+
if (num >= 0 && num <= 9)
61+
return (char)(num + '0');
62+
else
63+
return (char)(num - 10 + 'A');
64+
}
65+
}

Conversions/DecimalToBinary.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* This class converts a Decimal number to a Binary number
5+
*
6+
* @author Unknown
7+
*
8+
*/
9+
class DecimalToBinary
10+
{
11+
/**
12+
* Main Method
13+
*
14+
* @param args Command Line Arguments
15+
*/
16+
public static void main(String args[])
17+
{
18+
Scanner sc=new Scanner(System.in);
19+
int n,k,s=0,c=0,d;
20+
System.out.print("Decimal number: ");
21+
n=sc.nextInt();
22+
k=n;
23+
while(k!=0)
24+
{
25+
d=k%2;
26+
s=s+d*(int)Math.pow(10,c++);
27+
k/=2;
28+
}//converting decimal to binary
29+
System.out.println("Binary equivalent:"+s);
30+
sc.close();
31+
}
32+
}

Conversions/DecimalToHexaDecimal.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.lang.StringBuilder;
2+
import java.util.Scanner;
3+
4+
class Test {
5+
private static final int sizeOfIntInHalfBytes = 8;
6+
private static final int numberOfBitsInAHalfByte = 4;
7+
private static final int halfByte = 0x0F;
8+
private static final char[] hexDigits = {
9+
'0', '1', '2', '3', '4', '5', '6', '7',
10+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
11+
};
12+
13+
public static String decToHex(int dec) {
14+
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
15+
hexBuilder.setLength(sizeOfIntInHalfBytes);
16+
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
17+
{
18+
int j = dec & halfByte;
19+
hexBuilder.setCharAt(i, hexDigits[j]);
20+
dec >>= numberOfBitsInAHalfByte;
21+
}
22+
return hexBuilder.toString();
23+
}
24+
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner(System.in);
27+
System.out.println("Write your Number to convert into HexaDecimal: ")
28+
int dec = 305445566;
29+
String hex = Integer.toHexString(dec);
30+
String hex = decToHex(dec);
31+
System.out.println(hex);
32+
}
33+
}

0 commit comments

Comments
 (0)