In this commit I have:
Added JavaDoc to every package except for "heaps"
This commit is contained in:
parent
94871b7e6a
commit
9411d5be56
7
.classpath
Normal file
7
.classpath
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry excluding="data_structures/" kind="src" path=""/>
|
||||
<classpathentry kind="src" path="data_structures"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/bin/
|
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Java</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
74
BinarySearch.java
Normal file
74
BinarySearch.java
Normal file
@ -0,0 +1,74 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Implements a Binary Search in Java
|
||||
*
|
||||
* @author unknown
|
||||
*/
|
||||
class BinarySearch
|
||||
{
|
||||
/**
|
||||
* This method implements the Binary Search
|
||||
*
|
||||
* @param array The array to make the binary search
|
||||
* @param key The number you are looking for
|
||||
* @param lb The lower bound
|
||||
* @param up The upper bound
|
||||
* @return the location of the key
|
||||
**/
|
||||
public static int BS(int array[], int key, int lb, int ub)
|
||||
{ if (lb>ub)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mid=(lb+ub)/2;
|
||||
|
||||
if (key<array[mid])
|
||||
{
|
||||
return (BS(array, key, lb, mid-1));
|
||||
}
|
||||
else if (key>array[mid])
|
||||
{
|
||||
return (BS(array, key, mid+1, ub));
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is the main method of Binary Search
|
||||
*
|
||||
* @author Unknown
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input=new Scanner(System.in);
|
||||
int array[]=new int[10] ;
|
||||
int key;
|
||||
|
||||
//Input
|
||||
System.out.println("Enter an array of 10 numbers : ");
|
||||
for (int i=0; i<10 ;i++ )
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
System.out.println("Enter the number to be searched : ");
|
||||
key=input.nextInt();
|
||||
|
||||
int index=BS(array, key, 0, 9);
|
||||
if (index!=-1)
|
||||
{
|
||||
System.out.println("Number found at index number : " + index);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Not found");
|
||||
}
|
||||
}
|
||||
}
|
32
BinaryToDecimal.java
Normal file
32
BinaryToDecimal.java
Normal file
@ -0,0 +1,32 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class converts a Binary number to a Decimal number
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class BinaryToDecimal
|
||||
{
|
||||
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%10;
|
||||
s+=d*(int)Math.pow(2,c++);
|
||||
k/=10;
|
||||
}
|
||||
System.out.println("Binary number:"+n);
|
||||
System.out.println("Decimal equivalent:"+s);
|
||||
}
|
||||
}
|
50
BubbleSort.java
Normal file
50
BubbleSort.java
Normal file
@ -0,0 +1,50 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class implements BubbleSort
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
class BubbleSort
|
||||
{
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
for(int j=i+1; j<6; j++)
|
||||
{
|
||||
if(array[j]>array[i])
|
||||
{
|
||||
int temp=array[j];
|
||||
array[j]=array[i];
|
||||
array[i]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
31
DecimalToBinary.java
Normal file
31
DecimalToBinary.java
Normal file
@ -0,0 +1,31 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class converts a Decimal number to a Binary number
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class DecimalToBinary
|
||||
{
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,s=0,c=0,d;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%2;
|
||||
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);
|
||||
}
|
||||
}
|
31
DecimalToOctal.java
Normal file
31
DecimalToOctal.java
Normal file
@ -0,0 +1,31 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class converts Decimal numbers to Octal Numbers
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Decimal_Octal
|
||||
{
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line Arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int n,k,d,s=0,c=0;
|
||||
n=sc.nextInt();
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%8;
|
||||
s+=d*(int)Math.pow(10,c++);
|
||||
k/=8;
|
||||
}
|
||||
System.out.println("Decimal number:"+n);
|
||||
System.out.println("Octal equivalent:"+s);
|
||||
}
|
||||
}
|
@ -1,7 +1,19 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This program will print out the factorial of any non-negative
|
||||
* number that you input into it.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class Factorial{
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
//Prompt user to enter integer
|
||||
@ -20,7 +32,12 @@ public class Factorial{
|
||||
}
|
||||
}
|
||||
|
||||
//Factorial method
|
||||
/**
|
||||
* Recursive Factorial Method
|
||||
*
|
||||
* @param n The number to factorial
|
||||
* @return The factorial of the number
|
||||
*/
|
||||
public static long factorial(int n){
|
||||
|
||||
if (n==0){
|
||||
|
@ -1,12 +1,27 @@
|
||||
/*
|
||||
* The Sieve of Eratosthenes is an algorithm used to find prime numbers, up to a given value.
|
||||
/**
|
||||
* 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];
|
||||
|
||||
|
@ -1,24 +1,22 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Heap Sort Algorithm. Implements MinHeap
|
||||
* Heap Sort Algorithm
|
||||
* Implements MinHeap
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class HeapSort {
|
||||
/**
|
||||
* array to store heap.
|
||||
*/
|
||||
/** Array to store heap */
|
||||
private int[] heap;
|
||||
/**
|
||||
* size of heap.
|
||||
*/
|
||||
/** The size of the heap */
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor
|
||||
*
|
||||
* @param heap
|
||||
* array of unordered integers
|
||||
* @param heap array of unordered integers
|
||||
*/
|
||||
public HeapSort(int[] heap) {
|
||||
this.setHeap(heap);
|
||||
@ -26,32 +24,28 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this.size with {@code length).
|
||||
* Setter for variable size
|
||||
*
|
||||
* @param length
|
||||
* integer length of heap
|
||||
* @param length new size
|
||||
*/
|
||||
private void setSize(int length) {
|
||||
this.size = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets Heap with {@code heap}.
|
||||
* Setter for variable heap
|
||||
*
|
||||
* @param heap
|
||||
* array of unordered elements
|
||||
* @param heap array of unordered elements
|
||||
*/
|
||||
private void setHeap(int[] heap) {
|
||||
this.heap = heap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps index of {@code first} with {@code second}.
|
||||
* Swaps index of first with second
|
||||
*
|
||||
* @param first
|
||||
* index to swap {@code second} with
|
||||
* @param second
|
||||
* index to swap {@code first} with
|
||||
* @param first First index to switch
|
||||
* @param second Second index to switch
|
||||
*/
|
||||
private void swap(int first, int second) {
|
||||
int temp = this.heap[first];
|
||||
@ -60,12 +54,10 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Heapifies subtree from {@code top} as root to {@code last} as last child.
|
||||
* Heapifies subtree from top as root to last as last child
|
||||
*
|
||||
* @param rootIndex
|
||||
* index of root
|
||||
* @param lastChild
|
||||
* index of last child
|
||||
* @param rootIndex index of root
|
||||
* @param lastChild index of last child
|
||||
*/
|
||||
private void heapSubtree(int rootIndex, int lastChild) {
|
||||
int leftIndex = rootIndex * 2 + 1;
|
||||
@ -91,10 +83,9 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes heap with {@code root} as root.
|
||||
* Makes heap with root as root
|
||||
*
|
||||
* @param root
|
||||
* index of root of heap
|
||||
* @param root index of root of heap
|
||||
*/
|
||||
private void makeMinHeap(int root) {
|
||||
int leftIndex = root * 2 + 1;
|
||||
@ -111,9 +102,9 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the root of this.heap.
|
||||
* Gets the root of heap
|
||||
*
|
||||
* @return root of this.heap
|
||||
* @return root of heap
|
||||
*/
|
||||
private int getRoot() {
|
||||
this.swap(0, this.size - 1);
|
||||
@ -123,9 +114,9 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts this.heap with heap sort; displays ordered elements to console.
|
||||
* Sorts heap with heap sort; displays ordered elements to console.
|
||||
*
|
||||
* @return {@code sorted} array of sorted elements
|
||||
* @return sorted array of sorted elements
|
||||
*/
|
||||
public final int[] sort() {
|
||||
this.makeMinHeap(0); // make min heap using index 0 as root.
|
||||
@ -140,7 +131,7 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets input to sort.
|
||||
* Gets input to sort
|
||||
*
|
||||
* @return unsorted array of integers to sort
|
||||
*/
|
||||
@ -157,10 +148,9 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints elements in heap.
|
||||
* Prints elements in heap
|
||||
*
|
||||
* @param heap
|
||||
* array representing heap
|
||||
* @param heap array representing heap
|
||||
*/
|
||||
public static void printData(int[] heap) {
|
||||
System.out.println("Sorted Elements:");
|
||||
@ -170,10 +160,9 @@ public class HeapSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method.
|
||||
* Main method
|
||||
*
|
||||
* @param args
|
||||
* the command line arguments
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int[] heap = getInput();
|
||||
|
49
InsertionSort.java
Normal file
49
InsertionSort.java
Normal file
@ -0,0 +1,49 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class implements Insertion Sort
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class InsertionSort
|
||||
{
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
int temp=array[i];
|
||||
int j=i-1;
|
||||
while(j>=0 && temp<array[j] )
|
||||
{
|
||||
array[j+1]=array[j];
|
||||
j--;
|
||||
}
|
||||
|
||||
array[j+1]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
55
InsertionSortInteger.java
Normal file
55
InsertionSortInteger.java
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* This is my implementation of an insertion sort.
|
||||
*
|
||||
* I decided to do this when i didn't feel comfortable enough about implementing
|
||||
* different types of sorts, so this is my trial and error to try and get myself to implement
|
||||
* the various sorts correctly.
|
||||
*
|
||||
* @author Kody C. Kendall
|
||||
*
|
||||
*/
|
||||
public class InsertionSortInteger {
|
||||
|
||||
|
||||
public int[] insertionIntArraySort(int[] initialArray){
|
||||
|
||||
int[] sortedArray = new int[initialArray.length];
|
||||
|
||||
//Marking first element as sorted.
|
||||
sortedArray[0] = initialArray[0];
|
||||
|
||||
//For each element in the initialArray
|
||||
for (int index = 1; index < initialArray.length; index++){
|
||||
|
||||
//Finding the last index that was sorted
|
||||
int lastSortedIndex = index;
|
||||
|
||||
//Extracting the next element to be compared w/ other sorted elements from initial array
|
||||
int extractedElement = initialArray[index];
|
||||
|
||||
//Compare the values of the sorted index to the current extractedElement
|
||||
for (lastSortedIndex = index; lastSortedIndex > 0; lastSortedIndex--){
|
||||
|
||||
//If our extracted element is smaller than element to the right, switch them.
|
||||
if (sortedArray[lastSortedIndex-1] > extractedElement){
|
||||
//move the element to the left of extractedElement to the right in sortedArray
|
||||
sortedArray[lastSortedIndex] = sortedArray[lastSortedIndex-1];
|
||||
//And move the current extractedElement to the left one (since it's smaller).
|
||||
sortedArray[lastSortedIndex-1] = extractedElement;
|
||||
}
|
||||
else{
|
||||
//insert element where it is.
|
||||
sortedArray[lastSortedIndex] = extractedElement;
|
||||
//Terminating loop since element is in the right spot.
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sortedArray;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class LinearSearch{
|
||||
//Main method
|
||||
/**
|
||||
* The main method
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
@ -27,7 +30,13 @@ public class LinearSearch{
|
||||
|
||||
}
|
||||
|
||||
//Linear search method
|
||||
/**
|
||||
* Linear search method
|
||||
*
|
||||
* @param list List to be searched
|
||||
* @param key Key being searched for
|
||||
* @return Location of the key
|
||||
*/
|
||||
public static int linearsearch(int[] list, int key){
|
||||
|
||||
for (int i = 0; i< list.length; i++){
|
||||
@ -37,7 +46,11 @@ public class LinearSearch{
|
||||
|
||||
} return -1;
|
||||
}
|
||||
//Helper method
|
||||
/**
|
||||
* Helper Method
|
||||
*
|
||||
* @param a array to be printed
|
||||
*/
|
||||
public static void printarray(int[] a){
|
||||
System.out.print("The array is: ");
|
||||
for( int d: a){
|
||||
|
@ -1,18 +1,22 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Merge Sort
|
||||
* This class implements MergeSort
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class MergeSort {
|
||||
/** Array for mergeSort*/
|
||||
private int[] array;
|
||||
/** Temp Merge Array*/
|
||||
private int[] tempMergArr;
|
||||
/** Length of the array*/
|
||||
private int length;
|
||||
|
||||
/**
|
||||
* Sorts {@code inputArr} with merge sort algorithm.
|
||||
* Sorts inputArr with merge sort algorithm
|
||||
*
|
||||
* @param inputArr
|
||||
* @param inputArr Array to be sorted
|
||||
*/
|
||||
public final void sort(int inputArr[]) {
|
||||
this.array = inputArr;
|
||||
@ -22,12 +26,10 @@ public class MergeSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions Array into recursively smaller pieces.
|
||||
* Partitions Array into recursively smaller pieces
|
||||
*
|
||||
* @param lowerIndex
|
||||
* lower bound to include in the first partition
|
||||
* @param higherIndex
|
||||
* upper bound to include in the third partition
|
||||
* @param lowerIndex lower bound to include in the first partition
|
||||
* @param higherIndex upper bound to include in the third partition
|
||||
*/
|
||||
private void mergeSort(int lowerIndex, int higherIndex) {
|
||||
if (lowerIndex < higherIndex) {
|
||||
@ -42,11 +44,11 @@ public class MergeSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges partitions.
|
||||
* Merges partitions
|
||||
*
|
||||
* @param lowerIndex
|
||||
* @param middle
|
||||
* @param higherIndex
|
||||
* @param lowerIndex The lower index
|
||||
* @param middle The middle index
|
||||
* @param higherIndex The higher index
|
||||
*/
|
||||
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
|
||||
for (int i = lowerIndex; i <= higherIndex; i++) {
|
||||
@ -74,7 +76,7 @@ public class MergeSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets input to sort.
|
||||
* Gets input to sort
|
||||
*
|
||||
* @return unsorted array of integers to sort
|
||||
*/
|
||||
@ -91,9 +93,9 @@ public class MergeSort {
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method.
|
||||
* Main Method
|
||||
*
|
||||
* @param args
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
int[] inputArr = getInput();
|
||||
|
@ -1,7 +1,19 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Implementation of QuickSort
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
public class Quicksort{
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
int[] array;
|
||||
@ -28,7 +40,13 @@ public class Quicksort{
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
//Quicksort Method
|
||||
/**
|
||||
* QuickSort method
|
||||
*
|
||||
* @param ar Array to perform QuickSort
|
||||
* @param start Start of the array
|
||||
* @param end End of the array
|
||||
*/
|
||||
public static void quicksort(int[] ar, int start, int end){
|
||||
int[] array;
|
||||
|
||||
@ -54,7 +72,13 @@ public class Quicksort{
|
||||
}
|
||||
}
|
||||
|
||||
//Helper methods
|
||||
/**
|
||||
* Helper Method 1 - Swaps elements of an array
|
||||
*
|
||||
* @param ar Array to be used
|
||||
* @param index1 Index 1 to be switched with Index 2
|
||||
* @param index2 Index 2 to be switched with Index 1
|
||||
*/
|
||||
public static void swap(int[] ar, int index1, int index2){
|
||||
|
||||
int temp = ar[index1];
|
||||
@ -62,6 +86,11 @@ public class Quicksort{
|
||||
ar[index2] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper Method 2 - Prints the elements of an array
|
||||
*
|
||||
* @param array Array to be printed
|
||||
*/
|
||||
public static void printarray(int[] array){
|
||||
|
||||
for (int data : array){
|
||||
|
47
ReverseString.java
Normal file
47
ReverseString.java
Normal file
@ -0,0 +1,47 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This method produces a reversed version of a string
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class ReverseString
|
||||
{
|
||||
|
||||
/**
|
||||
* This method reverses the string str and returns it
|
||||
* @param str String to be reversed
|
||||
* @return Reversed string
|
||||
*/
|
||||
static String reverseString(String str)
|
||||
{
|
||||
String reverse=" ";
|
||||
if(str.length()==1)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
else
|
||||
{
|
||||
reverse=reverse+str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
|
||||
return reverse;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
* @throws IOException Exception thrown because of BufferedReader
|
||||
*/
|
||||
public static void main(String args[]) throws IOException
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the string");
|
||||
String srr=br.readLine();
|
||||
System.out.println("Reverse="+reverseString(srr));
|
||||
}
|
||||
}
|
||||
|
52
SelectionSort.java
Normal file
52
SelectionSort.java
Normal file
@ -0,0 +1,52 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This class implements Selection Sort
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
class SelectionSort
|
||||
{
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int array[]=new int[6];
|
||||
Scanner input=new Scanner(System.in);
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
array[i]=input.nextInt();
|
||||
}
|
||||
|
||||
//Sorting
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
int min=i;
|
||||
for(int j=i+1; j<6; j++)
|
||||
{
|
||||
if(array[j]<array[min])
|
||||
{
|
||||
min=j;
|
||||
}
|
||||
}
|
||||
int temp=array[i];
|
||||
array[i]=array[min];
|
||||
array[min]=temp;
|
||||
}
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
System.out.print(array[i]+"\t");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
22
bfs.java
22
bfs.java
@ -1,7 +1,20 @@
|
||||
import java.util.*;
|
||||
|
||||
public class App{
|
||||
/**
|
||||
* Implementation of a Breadth First Search
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class bfs{
|
||||
|
||||
/**
|
||||
* The BFS implemented in code to use.
|
||||
*
|
||||
* @param a Structure to perform the search on
|
||||
* @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
|
||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||
Arrays.fill(b,(byte)-1); //status initialization
|
||||
@ -10,7 +23,7 @@ public class App{
|
||||
0 = waiting
|
||||
1 = processed */
|
||||
|
||||
Queue <Integer> st=new LinkedList<>(); //operational stack
|
||||
Queue <Integer> st=new Queue<>(); //operational stack
|
||||
st.add(source); //assigning source
|
||||
while(!st.isEmpty()){
|
||||
b[st.peek()]=(byte)0; //assigning waiting status
|
||||
@ -26,6 +39,11 @@ public class App{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
Scanner in=new Scanner(System.in);
|
||||
int vertices=in.nextInt(),source=in.nextInt();
|
||||
|
@ -1,6 +1,19 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* You enter a string into this program, and it will return how
|
||||
* many words were in that particular string
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class CountTheWords
|
||||
{
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[])
|
||||
{
|
||||
System.out.println("Enter the string");
|
||||
|
@ -1,11 +1,35 @@
|
||||
//A binary tree is a data structure in which an element has two successors(children)
|
||||
//The left child is usually smaller than the parent, and the right child is usually bigger
|
||||
/**
|
||||
* This entire class is used to build a Binary Tree data structure.
|
||||
* There is the Node Class and the Tree Class, both explained below.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* This class implements the nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Node{
|
||||
/** Data for the node */
|
||||
public int data;
|
||||
/** The Node to the left of this one */
|
||||
public Node left;
|
||||
/** The Node to the right of this one */
|
||||
public Node right;
|
||||
/** The parent of this node */
|
||||
public Node parent;
|
||||
|
||||
/**
|
||||
* Constructor of Node
|
||||
*
|
||||
* @param value Value to put in the node
|
||||
*/
|
||||
public Node(int value){
|
||||
data = value;
|
||||
left = null;
|
||||
@ -14,13 +38,33 @@ class Node{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Tree{
|
||||
/** The root of the Binary Tree */
|
||||
private Node root;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public Tree(){
|
||||
root = null;
|
||||
}
|
||||
//Returns the node if it finds it, otherwise returns the parent
|
||||
|
||||
/**
|
||||
* Method to find a Node with a certain value
|
||||
*
|
||||
* @param key Value being looked for
|
||||
* @return The node if it finds it, otherwise returns the parent
|
||||
*/
|
||||
public Node find(int key){
|
||||
Node current = root;
|
||||
Node last = root;
|
||||
@ -37,7 +81,11 @@ class Tree{
|
||||
return last;
|
||||
}
|
||||
|
||||
//Inserts the given value
|
||||
/**
|
||||
* Inserts certain value into the Binary Tree
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void put(int value){
|
||||
Node newNode = new Node(value);
|
||||
if(root == null)
|
||||
@ -60,7 +108,12 @@ class Tree{
|
||||
}
|
||||
}
|
||||
|
||||
//Deletes the given value
|
||||
/**
|
||||
* Deletes a given value from the Binary Tree
|
||||
*
|
||||
* @param value Value to be deleted
|
||||
* @return If the value was deleted
|
||||
*/
|
||||
public boolean remove(int value){
|
||||
//temp is the node to be deleted
|
||||
Node temp = find(value);
|
||||
@ -84,34 +137,34 @@ class Tree{
|
||||
|
||||
//Two children
|
||||
else if(temp.left != null && temp.right != null){
|
||||
Node succesor = findSuccesor(temp);
|
||||
Node successor = findSuccessor(temp);
|
||||
|
||||
//The left tree of temp is made the left tree of the successor
|
||||
succesor.left = temp.left;
|
||||
succesor.left.parent = succesor;
|
||||
successor.left = temp.left;
|
||||
successor.left.parent = successor;
|
||||
|
||||
//If the successor has a right child, the child's grandparent is it's new parent
|
||||
if(succesor.right != null && succesor.parent != temp){
|
||||
succesor.right.parent = succesor.parent;
|
||||
succesor.parent.left = succesor.right;
|
||||
succesor.right = temp.right;
|
||||
succesor.right.parent = succesor;
|
||||
if(successor.right != null && successor.parent != temp){
|
||||
successor.right.parent = successor.parent;
|
||||
successor.parent.left = successor.right;
|
||||
successor.right = temp.right;
|
||||
successor.right.parent = successor;
|
||||
}
|
||||
if(temp == root){
|
||||
succesor.parent = null;
|
||||
root = succesor;
|
||||
successor.parent = null;
|
||||
root = successor;
|
||||
return true;
|
||||
}
|
||||
|
||||
//If you're not deleting the root
|
||||
else{
|
||||
succesor.parent = temp.parent;
|
||||
successor.parent = temp.parent;
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
if(temp.parent.data < temp.data)
|
||||
temp.parent.right = succesor;
|
||||
temp.parent.right = successor;
|
||||
else
|
||||
temp.parent.left = succesor;
|
||||
temp.parent.left = successor;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -148,8 +201,14 @@ class Tree{
|
||||
}
|
||||
}
|
||||
|
||||
//Move right once and go left down the tree as far as you can
|
||||
public Node findSuccesor(Node n){
|
||||
/**
|
||||
* This method finds the Successor to the Node given.
|
||||
* Move right once and go left down the tree as far as you can
|
||||
*
|
||||
* @param n Node that you want to find the Successor of
|
||||
* @return The Successor of the node
|
||||
*/
|
||||
public Node findSuccessor(Node n){
|
||||
if(n.right == null)
|
||||
return n;
|
||||
Node current = n.right;
|
||||
@ -161,11 +220,20 @@ class Tree{
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root of the Binary Tree
|
||||
*
|
||||
* @return the root of the Binary Tree
|
||||
*/
|
||||
public Node getRoot(){
|
||||
return root;
|
||||
}
|
||||
|
||||
//Prints leftChild - root - rightChild
|
||||
/**
|
||||
* Prints leftChild - root - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void inOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
inOrder(localRoot.left);
|
||||
@ -173,7 +241,12 @@ class Tree{
|
||||
inOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
//Prints root - leftChild - rightChild
|
||||
|
||||
/**
|
||||
* Prints root - leftChild - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void preOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
System.out.print(localRoot.data + " ");
|
||||
@ -181,7 +254,12 @@ class Tree{
|
||||
preOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
//Prints rightChild - leftChild - root
|
||||
|
||||
/**
|
||||
* Prints rightChild - leftChild - root
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void postOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
postOrder(localRoot.left);
|
||||
|
@ -1,19 +1,38 @@
|
||||
/*
|
||||
* A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
|
||||
* With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited.
|
||||
* This is an example of a double ended, doubly linked list.
|
||||
* Each link references the next link and the previous one.
|
||||
/**
|
||||
* This class implements a DoublyLinkedList. This is done using the classes
|
||||
* LinkedList and Link.
|
||||
*
|
||||
* A linked list is simplar to an array, it holds values. However,
|
||||
* links in a linked list do not have indees. With a linked list
|
||||
* you do not need to predetermine it's size as it grows and shrinks
|
||||
* as it is edited. This is an example of a double ended, doubly
|
||||
* linked list. Each link references the next link and the previous
|
||||
* one.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class LinkedList{
|
||||
private Link head; //Head refers to the front of the list
|
||||
private Link tail; //Tail refers to the back of the list
|
||||
|
||||
public LinkedList(){
|
||||
class DoublyLinkedList{
|
||||
/** Head refers to the front of the list */
|
||||
private Link head;
|
||||
/** Tail refers to the back of the list */
|
||||
private Link tail;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public DoublyLinkedList(){
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
public void insertHead(int x){ //Insert an element at the head
|
||||
/**
|
||||
* Insert an element at the head
|
||||
*
|
||||
* @param x Element to be inserted
|
||||
*/
|
||||
public void insertHead(int x){
|
||||
Link newLink = new Link(x); //Create a new link with a value attached to it
|
||||
if(isEmpty()) //Set the first element added to be the tail
|
||||
tail = newLink;
|
||||
@ -23,6 +42,11 @@ class LinkedList{
|
||||
head = newLink; // newLink(head) <--> oldhead
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an element at the tail
|
||||
*
|
||||
* @param x Element to be inserted
|
||||
*/
|
||||
public void insertTail(int x){
|
||||
Link newLink = new Link(x);
|
||||
newLink.next = null; // currentTail(tail) newlink -->
|
||||
@ -31,7 +55,12 @@ class LinkedList{
|
||||
tail = newLink; // oldTail <--> newLink(tail) -->
|
||||
}
|
||||
|
||||
public Link deleteHead(){ //Delete the element at the head
|
||||
/**
|
||||
* Delete the element at the head
|
||||
*
|
||||
* @return The new head
|
||||
*/
|
||||
public Link deleteHead(){
|
||||
Link temp = head;
|
||||
head = head.next; // oldHead <--> 2ndElement(head)
|
||||
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
|
||||
@ -40,6 +69,11 @@ class LinkedList{
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the element at the tail
|
||||
*
|
||||
* @return The new tail
|
||||
*/
|
||||
public Link deleteTail(){
|
||||
Link temp = tail;
|
||||
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
||||
@ -47,6 +81,12 @@ class LinkedList{
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the element from somewhere in the list
|
||||
*
|
||||
* @param x element to be deleted
|
||||
* @return Link deleted
|
||||
*/
|
||||
public Link delete(int x){
|
||||
Link current = head;
|
||||
|
||||
@ -66,6 +106,11 @@ class LinkedList{
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts element and reorders
|
||||
*
|
||||
* @param x Element to be added
|
||||
*/
|
||||
public void insertOrdered(int x){
|
||||
Link newLink = new Link(x);
|
||||
Link current = head;
|
||||
@ -86,10 +131,18 @@ class LinkedList{
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty(){ //Returns true if list is empty
|
||||
/**
|
||||
* Returns true if list is empty
|
||||
*
|
||||
* @return true if list is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(head == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints contents of the list
|
||||
*/
|
||||
public void display(){ //Prints contents of the list
|
||||
Link current = head;
|
||||
while(current!=null){
|
||||
@ -100,24 +153,44 @@ class LinkedList{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is used to implement the nodes of the
|
||||
* linked list.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Link{
|
||||
/** Value of node */
|
||||
public int value;
|
||||
public Link next; //This points to the link in front of the new link
|
||||
public Link previous; //This points to the link behind the new link
|
||||
/** This points to the link in front of the new link */
|
||||
public Link next;
|
||||
/** This points to the link behind the new link */
|
||||
public Link previous;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param value Value of node
|
||||
*/
|
||||
public Link(int value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the node
|
||||
*/
|
||||
public void displayLink(){
|
||||
System.out.print(value+" ");
|
||||
}
|
||||
}
|
||||
|
||||
//Example
|
||||
public class DoublyLinkedList{
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
LinkedList myList = new LinkedList();
|
||||
DoublyLinkedList myList = new DoublyLinkedList();
|
||||
|
||||
myList.insertHead(13);
|
||||
myList.insertHead(7);
|
||||
|
@ -1,21 +1,40 @@
|
||||
/*
|
||||
/**
|
||||
* This class implements a PriorityQueue.
|
||||
*
|
||||
* A priority queue adds elements into positions based on their priority.
|
||||
* So the most important elements are placed at the front/on the top.
|
||||
* In this example I give numbers that are bigger, a higher priority.
|
||||
* Queues in theory have no fixed size but when using an array implementation it does.
|
||||
* Queues in theory have no fixed size but when using an array
|
||||
* implementation it does.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class PriorityQueue{
|
||||
/** The max size of the queue */
|
||||
private int maxSize;
|
||||
/** The array for the queue */
|
||||
private int[] queueArray;
|
||||
/** How many items are in the queue */
|
||||
private int nItems;
|
||||
|
||||
public PriorityQueue(int size){ //Constructor
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param size Size of the queue
|
||||
*/
|
||||
public PriorityQueue(int size){
|
||||
maxSize = size;
|
||||
queueArray = new int[size];
|
||||
nItems = 0;
|
||||
}
|
||||
|
||||
public void insert(int value){ //Inserts an element in it's appropriate place
|
||||
/**
|
||||
* Inserts an element in it's appropriate place
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void insert(int value){
|
||||
if(nItems == 0){
|
||||
queueArray[0] = value;
|
||||
}
|
||||
@ -30,28 +49,64 @@ class PriorityQueue{
|
||||
nItems++;
|
||||
}
|
||||
|
||||
public int remove(){ //Remove the element from the front of the queue
|
||||
/**
|
||||
* Remove the element from the front of the queue
|
||||
*
|
||||
* @return The element removed
|
||||
*/
|
||||
public int remove(){
|
||||
return queueArray[--nItems];
|
||||
}
|
||||
|
||||
public int peek(){ //Checks what's at the front of the queue
|
||||
/**
|
||||
* Checks what's at the front of the queue
|
||||
*
|
||||
* @return element at the front of the queue
|
||||
*/
|
||||
public int peek(){
|
||||
return queueArray[nItems-1];
|
||||
}
|
||||
|
||||
public boolean isEmpty(){ //Returns true is the queue is empty
|
||||
/**
|
||||
* Returns true if the queue is empty
|
||||
*
|
||||
* @return true if the queue is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(nItems == 0);
|
||||
}
|
||||
|
||||
public boolean isFull(){ //Returns true is the queue is full
|
||||
/**
|
||||
* Returns true if the queue is full
|
||||
*
|
||||
* @return true if the queue is full
|
||||
*/
|
||||
public boolean isFull(){
|
||||
return(nItems == maxSize);
|
||||
}
|
||||
|
||||
public int getSize(){ //Returns the number of elements in the queue
|
||||
/**
|
||||
* Returns the number of elements in the queue
|
||||
*
|
||||
* @return number of elements in the queue
|
||||
*/
|
||||
public int getSize(){
|
||||
return nItems;
|
||||
}
|
||||
}
|
||||
//Example
|
||||
|
||||
/**
|
||||
* This class implements the PriorityQueue class above.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class PriorityQueues{
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
PriorityQueue myQueue = new PriorityQueue(4);
|
||||
myQueue.insert(10);
|
||||
|
@ -1,16 +1,31 @@
|
||||
/*
|
||||
/**
|
||||
* This implements Queues by using the class Queue.
|
||||
*
|
||||
* A queue data structure functions the same as a real world queue.
|
||||
* The elements that are added first are the first to be removed.
|
||||
* New elements are added to the back/rear of the queue.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Queue{
|
||||
/** Max size of the queue */
|
||||
private int maxSize;
|
||||
/** The array representing the queue */
|
||||
private int[] queueArray;
|
||||
/** Front of the queue */
|
||||
private int front;
|
||||
/** Rear of the queue */
|
||||
private int rear;
|
||||
/** How many items are in the queue */
|
||||
private int nItems;
|
||||
|
||||
public Queue(int size){ //Constructor
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param size Size of the new queue
|
||||
*/
|
||||
public Queue(int size){
|
||||
maxSize = size;
|
||||
queueArray = new int[size];
|
||||
front = 0;
|
||||
@ -18,7 +33,13 @@ class Queue{
|
||||
nItems = 0;
|
||||
}
|
||||
|
||||
public boolean insert(int x){ //Inserts an element at the rear of the queue
|
||||
/**
|
||||
* Inserts an element at the rear of the queue
|
||||
*
|
||||
* @param x element to be added
|
||||
* @return True if the element was added successfully
|
||||
*/
|
||||
public boolean insert(int x){
|
||||
if(isFull())
|
||||
return false;
|
||||
if(rear == maxSize-1) //If the back of the queue is the end of the array wrap around to the front
|
||||
@ -29,6 +50,11 @@ class Queue{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element from the front of the queue
|
||||
*
|
||||
* @return the new front of the queue
|
||||
*/
|
||||
public int remove(){ //Remove an element from the front of the queue
|
||||
if(isEmpty()){
|
||||
System.out.println("Queue is empty");
|
||||
@ -42,28 +68,64 @@ class Queue{
|
||||
return temp;
|
||||
}
|
||||
|
||||
public int peekFront(){ //Checks what's at the front of the queue
|
||||
/**
|
||||
* Checks what's at the front of the queue
|
||||
*
|
||||
* @return element at the front of the queue
|
||||
*/
|
||||
public int peekFront(){
|
||||
return queueArray[front];
|
||||
}
|
||||
|
||||
public int peekRear(){ //Checks what's at the rear of the queue
|
||||
/**
|
||||
* Checks what's at the rear of the queue
|
||||
*
|
||||
* @return element at the rear of the queue
|
||||
*/
|
||||
public int peekRear(){
|
||||
return queueArray[rear];
|
||||
}
|
||||
|
||||
public boolean isEmpty(){ //Returns true is the queue is empty
|
||||
/**
|
||||
* Returns true if the queue is empty
|
||||
*
|
||||
* @return true if the queue is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(nItems == 0);
|
||||
}
|
||||
|
||||
public boolean isFull(){ //Returns true is the queue is full
|
||||
/**
|
||||
* Returns true if the queue is full
|
||||
*
|
||||
* @return true if the queue is full
|
||||
*/
|
||||
public boolean isFull(){
|
||||
return(nItems == maxSize);
|
||||
}
|
||||
|
||||
public int getSize(){ //Returns the number of elements in the queue
|
||||
/**
|
||||
* Returns the number of elements in the queue
|
||||
*
|
||||
* @return number of elements in the queue
|
||||
*/
|
||||
public int getSize(){
|
||||
return nItems;
|
||||
}
|
||||
}
|
||||
//Example
|
||||
|
||||
/**
|
||||
* This class is the example for the Queue class
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class Queues{
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
Queue myQueue = new Queue(4);
|
||||
myQueue.insert(10);
|
||||
|
@ -1,58 +1,78 @@
|
||||
/*
|
||||
* A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
|
||||
* With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited.
|
||||
* This is an example of a singly linked list. Elements can only be added/removed at the head/front of the list.
|
||||
/**
|
||||
* This class implements a SinglyLinked List. This is done
|
||||
* using SinglyLinkedList class and a LinkForLinkedList Class.
|
||||
*
|
||||
* A linked list is implar to an array, it hold values.
|
||||
* However, links in a linked list do not have indexes. With
|
||||
* a linked list you do not need to predetermine it's size as
|
||||
* it gorws and shrinks as it is edited. This is an example of
|
||||
* a singly linked list. Elements can only be added/removed
|
||||
* at the head/front of the list.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class LinkedList{
|
||||
private Link head; //Head refers to the front of the list
|
||||
class SinglyLinkedList{
|
||||
/**Head refered to the front of the list */
|
||||
private LinkForLinkedList head;
|
||||
|
||||
public LinkedList(){
|
||||
/**
|
||||
* Constructor of SinglyLinkedList
|
||||
*/
|
||||
public SinglyLinkedList(){
|
||||
head = null;
|
||||
}
|
||||
|
||||
public void insertHead(int x){ //Insert an element at the head
|
||||
Link newLink = new Link(x); //Create a new link with a value attached to it
|
||||
/**
|
||||
* This method inserts an element at the head
|
||||
*
|
||||
* @param x Element to be added
|
||||
*/
|
||||
public void insertHead(int x){
|
||||
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
|
||||
newLink.next = head; //Set the new link to point to the current head
|
||||
head = newLink; //Now set the new link to be the head
|
||||
}
|
||||
|
||||
public Link deleteHead(){ //Delete the element at the head
|
||||
Link temp = head;
|
||||
/**
|
||||
* This method deletes an element at the head
|
||||
*
|
||||
* @return The element deleted
|
||||
*/
|
||||
public LinkForLinkedList deleteHead(){
|
||||
LinkForLinkedList temp = head;
|
||||
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
|
||||
return temp;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){ //Returns true if list is empty
|
||||
/**
|
||||
* Checks if the list is empty
|
||||
*
|
||||
* @return true is list is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(head == null);
|
||||
}
|
||||
|
||||
public void display(){ //Prints contents of the list
|
||||
Link current = head;
|
||||
/**
|
||||
* Prints contents of the list
|
||||
*/
|
||||
public void display(){
|
||||
LinkForLinkedList current = head;
|
||||
while(current!=null){
|
||||
current.displayLink();
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
class Link{
|
||||
public int value;
|
||||
public Link next; //This is what the link will point to
|
||||
|
||||
public Link(int valuein){
|
||||
value = valuein;
|
||||
}
|
||||
|
||||
public void displayLink(){
|
||||
System.out.print(value+" ");
|
||||
}
|
||||
}
|
||||
|
||||
//Example
|
||||
public class SinglyLinkedList{
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
LinkedList myList = new LinkedList();
|
||||
SinglyLinkedList myList = new SinglyLinkedList();
|
||||
|
||||
System.out.println(myList.isEmpty()); //Will print true
|
||||
|
||||
@ -67,3 +87,35 @@ public class SinglyLinkedList{
|
||||
myList.display(); // 7(head) --> 5
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is the nodes of the SinglyLinked List.
|
||||
* They consist of a vlue and a pointer to the node
|
||||
* after them.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class LinkForLinkedList{
|
||||
/** The value of the node */
|
||||
public int value;
|
||||
/** Point to the next node */
|
||||
public LinkForLinkedList next; //This is what the link will point to
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param valuein Value to be put in the node
|
||||
*/
|
||||
public LinkForLinkedList(int valuein){
|
||||
value = valuein;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the value of the node
|
||||
*/
|
||||
public void displayLink(){
|
||||
System.out.print(value+" ");
|
||||
}
|
||||
|
||||
}
|
@ -1,62 +1,131 @@
|
||||
/*
|
||||
*A stack is exactly what it sounds like. An element gets added to top of the stack and only the element on the top may be removed.
|
||||
*This is an example of an array implementation of a Stack. So an element can only be added/removed from the end of the array.
|
||||
*In theory stacks have no fixed size, but with an array implementation it does.
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class implements a Stack using two different implementations.
|
||||
* Stack is used with a regular array and Stack2 uses an ArrayList.
|
||||
*
|
||||
* A stack is exactly what it sounds like. An element gets added to the top of
|
||||
* the stack and only the element on the top may be removed. This is an example
|
||||
* of an array implementation of a Stack. So an element can only be added/removed
|
||||
* from the end of the array. In theory stack have no fixed size, but with an
|
||||
* array implementation it does.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Stack{
|
||||
/** The max size of the Stack */
|
||||
private int maxSize;
|
||||
/** The array representation of the Stack */
|
||||
private int[] stackArray;
|
||||
/** The top of the stack */
|
||||
private int top;
|
||||
|
||||
public Stack(int size){ //Constructor
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param size Size of the Stack
|
||||
*/
|
||||
public Stack(int size){
|
||||
maxSize = size;
|
||||
stackArray = new int[maxSize];
|
||||
top = -1;
|
||||
}
|
||||
|
||||
public void push(int value){ //Adds an element to the top of the stack
|
||||
/**
|
||||
* Adds an element to the top of the stack
|
||||
*
|
||||
* @param value The element added
|
||||
*/
|
||||
public void push(int value){
|
||||
top++;
|
||||
stackArray[top] = value;
|
||||
}
|
||||
|
||||
public int pop(){ //Removes the top element of the stack and returns the value you've removed
|
||||
/**
|
||||
* Removes the top element of the stack and returns the value you've removed
|
||||
*
|
||||
* @return value popped off the Stack
|
||||
*/
|
||||
public int pop(){
|
||||
return stackArray[top--];
|
||||
}
|
||||
|
||||
public int peek(){ //Returns the element at the top of the stack
|
||||
/**
|
||||
* Returns the element at the top of the stack
|
||||
*
|
||||
* @return element at the top of the stack
|
||||
*/
|
||||
public int peek(){
|
||||
return stackArray[top];
|
||||
}
|
||||
|
||||
public boolean isEmpty(){ //Returns true if the stack is empty
|
||||
/**
|
||||
* Returns true if the stack is empty
|
||||
*
|
||||
* @return true if the stack is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(top == -1);
|
||||
}
|
||||
|
||||
public boolean isFull(){ //Returns true if the stack is full
|
||||
/**
|
||||
* Returns true if the stack is full
|
||||
*
|
||||
* @return true if the stack is full
|
||||
*/
|
||||
public boolean isFull(){
|
||||
return(top+1 == maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes everything in the Stack
|
||||
*
|
||||
* Doesn't delete elements in the array
|
||||
* but if you call push method after calling
|
||||
* makeEmpty it will overwrite previous
|
||||
* values
|
||||
*/
|
||||
public void makeEmpty(){ //Doesn't delete elements in the array but if you call
|
||||
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* This is ArrayList Implementation of stack , Where size is not
|
||||
a problem we can extend the stack as much as we want*/
|
||||
/**
|
||||
* This is an ArrayList Implementation of stack, Where size is not
|
||||
* a problem we can extend the stack as much as we want.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Stack2{
|
||||
|
||||
|
||||
/** ArrayList representation of the stack */
|
||||
ArrayList<Integer> stackList;
|
||||
|
||||
Stack2(){ //constructor
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Stack2(){
|
||||
stackList=new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
void push(int value){ //adds value to the end of list which is the top for stack
|
||||
/**
|
||||
* Adds value to the end of list which
|
||||
* is the top for stack
|
||||
*
|
||||
* @param value value to be added
|
||||
*/
|
||||
void push(int value){
|
||||
stackList.add(value);
|
||||
}
|
||||
|
||||
int pop(){ //pops last element of list which is indeed the top for Stack
|
||||
/**
|
||||
* Pops last element of list which is indeed
|
||||
* the top for Stack
|
||||
*
|
||||
* @return Element popped
|
||||
*/
|
||||
int pop(){
|
||||
|
||||
if(!isEmpty()){ // checks for an empty Stack
|
||||
|
||||
@ -71,7 +140,12 @@ class Stack2{
|
||||
|
||||
}
|
||||
|
||||
boolean isEmpty(){ //checks for empty Stack
|
||||
/**
|
||||
* Checks for empty Stack
|
||||
*
|
||||
* @return true if stack is empty
|
||||
*/
|
||||
boolean isEmpty(){
|
||||
if(stackList.isEmpty())
|
||||
return true;
|
||||
|
||||
@ -79,13 +153,28 @@ class Stack2{
|
||||
|
||||
}
|
||||
|
||||
int peek(){ //top element of stack
|
||||
/**
|
||||
* Top element of stack
|
||||
*
|
||||
* @return top element of stack
|
||||
*/
|
||||
int peek(){
|
||||
return stackList.get(stackList.size()-1);
|
||||
}
|
||||
}
|
||||
|
||||
//Example
|
||||
/**
|
||||
* This class implements the Stack and Stack2 created above
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class Stacks{
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
|
||||
//Populate the stack
|
||||
|
@ -84,7 +84,13 @@ public class MaxHeap implements Heap {
|
||||
|
||||
@Override
|
||||
public void deleteElement(int elementIndex) {
|
||||
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||
if (maxHeap.isEmpty())
|
||||
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");
|
||||
// The last element in heap replaces the one to be deleted
|
||||
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
|
||||
|
@ -87,7 +87,13 @@ public class MinHeap implements Heap {
|
||||
|
||||
@Override
|
||||
public void deleteElement(int elementIndex) {
|
||||
if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap");
|
||||
if (minHeap.isEmpty())
|
||||
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");
|
||||
// The last element in heap replaces the one to be deleted
|
||||
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
|
||||
|
20
dfs.java
20
dfs.java
@ -1,8 +1,21 @@
|
||||
import java.util.*;
|
||||
|
||||
public class App{
|
||||
/**
|
||||
* Implementation of a Depth First Search
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
public class dfs{
|
||||
|
||||
/**
|
||||
* Implementation in code of a DFS
|
||||
*
|
||||
* @param a structure to be DFS'ed
|
||||
* @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
|
||||
byte []b=new byte[vertices]; //flag container containing status of each vertices
|
||||
Arrays.fill(b,(byte)-1); //status initialization
|
||||
@ -27,6 +40,11 @@ public class App{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
Scanner in=new Scanner(System.in);
|
||||
int vertices=in.nextInt(),source=in.nextInt();
|
||||
|
Loading…
Reference in New Issue
Block a user