mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 08:17:33 +08:00
- Closed all Scanner and Buffered Readers
- Added Some Java Doc - ReverseString - took space out of between each letter so "Zachary" to "yrahcaZ" instead of "y r a h c aZ" - bfs - Trying to repair this class but need to research bfs more - dfs - Trying to repair this class but need to research dfs more - Added new Classes 1) OctalToBinary - Not done 2) BinaryToOctal - Not done 3) OctalToDecimal - Not done 4) Graphs -Added the dataStructure Graphs (Unfinished)
This commit is contained in:
@ -70,5 +70,6 @@ class BinarySearch
|
||||
{
|
||||
System.out.println("Not found");
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ class BinaryToDecimal
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
System.out.print("Binary number: ");
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
@ -26,7 +27,7 @@ class BinaryToDecimal
|
||||
s+=d*(int)Math.pow(2,c++);
|
||||
k/=10;
|
||||
}
|
||||
System.out.println("Binary number:"+n);
|
||||
System.out.println("Decimal equivalent:"+s);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
35
BinaryToOctal.java
Normal file
35
BinaryToOctal.java
Normal file
@ -0,0 +1,35 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Converts any Binary number to an Octal Number
|
||||
*
|
||||
* @author Zachary Jones
|
||||
*
|
||||
*/
|
||||
public class BinaryToOctal {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int b = sc.nextInt();
|
||||
System.out.println("Octal equivalent: " + convertBinaryToOctal(b));
|
||||
sc.close();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a binary number to
|
||||
* an octal number.
|
||||
*
|
||||
* @param b The binary number
|
||||
* @return The octal number
|
||||
*/
|
||||
public static int convertBinaryToOctal(int b) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -45,6 +45,6 @@ class BubbleSort
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ class DecimalToBinary
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,s=0,c=0,d;
|
||||
System.out.print("Decimal number: ");
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
@ -25,7 +26,7 @@ class DecimalToBinary
|
||||
s=s+d*(int)Math.pow(10,c++);
|
||||
k/=2;
|
||||
}//converting decimal to binary
|
||||
System.out.println("Decimal number:"+n);
|
||||
System.out.println("Binary equivalent:"+s);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ class Decimal_Octal
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
System.out.print("Decimal number: ");
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
@ -25,7 +26,8 @@ class Decimal_Octal
|
||||
s+=d*(int)Math.pow(10,c++);
|
||||
k/=8;
|
||||
}
|
||||
System.out.println("Decimal number:"+n);
|
||||
|
||||
System.out.println("Octal equivalent:"+s);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,8 @@ public class Factorial{
|
||||
//Output of factorial for any non-negative number
|
||||
System.out.println("The factorial of "+number+" will yield: "+factorial(number));
|
||||
}
|
||||
}
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,6 +44,6 @@ class InsertionSort
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
input.close();
|
||||
}
|
||||
}
|
@ -11,6 +11,12 @@
|
||||
public class InsertionSortInteger {
|
||||
|
||||
|
||||
/**
|
||||
* This method implements insertion sort by integer
|
||||
*
|
||||
* @param initialArray array to be sorted
|
||||
* @return sorted array
|
||||
*/
|
||||
public int[] insertionIntArraySort(int[] initialArray){
|
||||
|
||||
int[] sortedArray = new int[initialArray.length];
|
||||
|
@ -27,7 +27,8 @@ public class LinearSearch{
|
||||
//Output array and index of target element, if found
|
||||
printarray(myArray);
|
||||
System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key));
|
||||
|
||||
|
||||
input.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
34
OctalToBinary.java
Normal file
34
OctalToBinary.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Converts any Octal number to a Binary number
|
||||
*
|
||||
* @author Zachary Jones
|
||||
*
|
||||
*/
|
||||
public class OctalToBinary {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int o = sc.nextInt();
|
||||
System.out.println("Binary equivalent: " + convertOctalToBinary(o));
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts an octal number
|
||||
* to a binary number.
|
||||
*
|
||||
* @param o The octal number
|
||||
* @return The binary number
|
||||
*/
|
||||
public static int convertOctalToBinary(int o) {
|
||||
|
||||
}
|
||||
|
||||
}
|
34
OctalToDecimal.java
Normal file
34
OctalToDecimal.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Converts any Octal Number to a Decimal Number
|
||||
*
|
||||
* @author Zachary Jones
|
||||
*
|
||||
*/
|
||||
public class OctalToDecimal {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @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));
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts an octal number to
|
||||
* a decimal number.
|
||||
*
|
||||
* @param o The octal number
|
||||
* @return The decimal number
|
||||
*/
|
||||
public static int convertOctalToDecimal(int o) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -38,6 +38,8 @@ public class Quicksort{
|
||||
System.out.println("The sorted array is: ");
|
||||
printarray(array);
|
||||
System.out.println();
|
||||
|
||||
input.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ class ReverseString
|
||||
*/
|
||||
static String reverseString(String str)
|
||||
{
|
||||
String reverse=" ";
|
||||
String reverse="";
|
||||
if(str.length()==1)
|
||||
{
|
||||
return str;
|
||||
@ -42,6 +42,7 @@ class ReverseString
|
||||
System.out.println("Enter the string");
|
||||
String srr=br.readLine();
|
||||
System.out.println("Reverse="+reverseString(srr));
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ class SelectionSort
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
|
||||
input.close();
|
||||
}
|
||||
}
|
20
bfs.java
20
bfs.java
@ -15,7 +15,7 @@ public class bfs{
|
||||
* @param vertices The vertices to use
|
||||
* @param source The Source
|
||||
*/
|
||||
public static void bfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||
public static void bfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||
Arrays.fill(b,(byte)-1); //status initialization
|
||||
/* code status
|
||||
@ -23,17 +23,17 @@ public class bfs{
|
||||
0 = waiting
|
||||
1 = processed */
|
||||
|
||||
Queue <Integer> st=new Queue<>(); //operational stack
|
||||
st.add(source); //assigning source
|
||||
Stack st = new Stack(vertices); //operational stack
|
||||
st.push(source); //assigning source
|
||||
while(!st.isEmpty()){
|
||||
b[st.peek()]=(byte)0; //assigning waiting status
|
||||
System.out.println(st.element());
|
||||
int pop=st.element();
|
||||
System.out.println(st.peek());
|
||||
int pop=st.peek();
|
||||
b[pop]=(byte)1; //assigning processed status
|
||||
st.remove(); //removing head of the queue
|
||||
st.pop(); //removing head of the queue
|
||||
for(int i=0;i<vertices;i++){
|
||||
if(a[pop][i]!=0 && b[i]!=(byte)0 && b[i]!=(byte)1 ){
|
||||
st.add(i);
|
||||
st.push(i);
|
||||
b[i]=(byte)0; //assigning waiting status
|
||||
}}}
|
||||
}
|
||||
@ -56,5 +56,7 @@ public class bfs{
|
||||
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
|
||||
}
|
||||
}
|
||||
bfs(a,vertices,source); //function call
|
||||
}}
|
||||
bfsImplement(a,vertices,source); //function call
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
@ -28,5 +28,6 @@ class CountTheWords
|
||||
}
|
||||
}
|
||||
System.out.println("Number of words in the string = "+count);
|
||||
sc.close();
|
||||
}
|
||||
}
|
32
data_structures/Graphs.java
Normal file
32
data_structures/Graphs.java
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
/**
|
||||
* This class implements the Graph data structure
|
||||
* using the classes Graph and Graphs.
|
||||
*
|
||||
* @author Zachary Jones
|
||||
*
|
||||
*/
|
||||
class Graph {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is used to test the Graph
|
||||
* class above.
|
||||
*
|
||||
* @author Zachary Jones
|
||||
*
|
||||
*/
|
||||
|
||||
public class Graphs {
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -88,7 +88,6 @@ public class MaxHeap implements Heap {
|
||||
try {
|
||||
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||
} catch (EmptyHeapException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
||||
|
@ -91,7 +91,6 @@ public class MinHeap implements Heap {
|
||||
try {
|
||||
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||
} catch (EmptyHeapException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range");
|
||||
|
10
dfs.java
10
dfs.java
@ -16,7 +16,7 @@ public class dfs{
|
||||
* @param vertices The vertices
|
||||
* @param source The source
|
||||
*/
|
||||
public static void dfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||
public static void dfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
|
||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||
Arrays.fill(b,(byte)-1); //status initialization
|
||||
/* code status
|
||||
@ -25,7 +25,7 @@ public class dfs{
|
||||
1 = processed */
|
||||
|
||||
|
||||
Stack <Integer> st=new Stack<>(); //operational stack
|
||||
Stack st=new Stack(vertices); //operational stack
|
||||
st.push(source); //assigning source
|
||||
while(!st.isEmpty()){
|
||||
b[st.peek()]=(byte)0; //assigning waiting status
|
||||
@ -57,5 +57,7 @@ public class dfs{
|
||||
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
|
||||
}
|
||||
}
|
||||
bfs(a,vertices,source); //function call
|
||||
}}
|
||||
dfsImplement(a,vertices,source); //function call
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user