Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Dynamic Programming/LongestCommonSubsequenceLength
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Scanner;
class LongestCommonSubsequenceLength{
public static void main(String[]args){
//initialize scanner
Scanner kb=new Scanner(System.in);
//prompt user for input
System.out.println("Provide 2 Strings");
//take in 2 strings
String s1=kb.nextLine();
String s2=kb.nextLine();
//make 2d array for strings (with space for zeroes to pad first row and column)
int[][]a=new int[s1.length()+1][s2.length()+1];

//pad firs column with 0s
for(int i=0; i<=s1.length(); i++)
a[i][0]=0;
//pad first column with 0s
for(int j=0; j<=s2.length(); j++)
a[0][j]=0;
/*now loop over the entire 2d matrix and use the following
recurrence relation: a[i][j]=max(a[i-1][j], a[i][j-1])
or if the current i,j position has a matching character include a[i-1][j-1] in that calculation.

Why does this work? We take either the longest common subsequence up to character j of string 2 or up to character i of string 1, whichever is better, because each occurrence must be in order, the greater of these two must be best up to index i in string 1 and index j in string 2.

However, if the ith character of str1 matches the jth character of str2, then we must consider what happens if the optimal solution through character i-1 and j-1 was the same as i-1 and j or j-1 and i. Clearly, in either of these cases, by taking the ith character of string 1, which matches the jth character of string 2, we are guaranteed to be better off taking i and j matching character next, because the alternative would be to wait for either i and j+1 or j and i+1 to match, both of which are at least no better than having the same count through i and j.
*/
for(int i=0; i<s1.length(); i++)
for(int j=0; j<s2.length(); j++)
if(s1.charAt(i)==s2.charAt(j))
a[i+1][j+1]=Math.max(Math.max(a[i][j]+1, a[i][j+1]),a[i+1][j]);
else
a[i+1][j+1]=Math.max(a[i][j+1], a[i+1][j]);

/*Then we just print out whatever is in the last position in our array since it's the longest common subsequence up through the final characters of each string*/
System.out.println(a[s1.length()][s2.length()]);

}
}
80 changes: 80 additions & 0 deletions Sorting Algorithm/heapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.Scanner;
class heapSort
{
//we sort using heap sort
public static void heapsort(int[]arr){
//first we build our max heap
for(int i=(arr.length-1)/2; i>=1; i--){
heapify(arr, i, arr.length);
}
/*then we keep swapping extracting max and swapping it to the end of the array.
thus we retain the invariant that the ith element from the end is the ith largest element.
So, the array must therefore be sorted in increasing order*/
for(int i=arr.length-1; i>=2; i--){
//extract min and relocate the displaced element after the swap using heapify
int temp=arr[1];
arr[1]=arr[i];
arr[i]=temp;
heapify(arr, 1, i);
}
}
/*recursively check the left and right children, swapping the currend node down in the heap if smaller than one or more of its children*/
public static void heapify(int[]arr, int pos, int len){
int max=pos;
//if the left child is larger it becomes largest
if(pos*2<len)
if(arr[pos*2]>arr[max])
max=pos*2;
if(pos*2+1<len)
if(arr[pos*2+1]>arr[max])//but if right child is larger than both, it becomes largest
max=pos*2+1;
if(max!=pos){ /*if the largest changed, we swap the elements and then heapify the subtree of which our original pos for this pass is the root node*/
int temp=arr[pos];
arr[pos]=arr[max];
arr[max]=temp;
heapify(arr, max, len);
}
}
public static void main(String[]args){
//initialize scanner
Scanner kb=new Scanner(System.in);
//while user has not quit
while(true){
//prompt user for array size
System.out.println("Enter an array size");
//get length of new array
int len=kb.nextInt();
//make new array of length len+1 to work with a heap indexed at 1 for simplicity
int[]arr=new int[len+1];
/*and we make the first element effectively -infinity (so we will not have to worry about us accidentally sorting this element)*/
arr[0]=Integer.MAX_VALUE;
//prompt user for array values
System.out.println("Enter "+len+" values for your array");
//fill the array with input values
for(int i=1; i<=len; i++)
arr[i]=kb.nextInt();
//run the sort (java arrays are pass by reference)
heapsort(arr);
//print out the sorted array
for(int i=1; i<=len; i++)
if(i!=len)
System.out.print(arr[i]+" ");
else
System.out.println(arr[i]);

//ask user to go again
System.out.println("Would you like to sort again? (yes/no)");
String response=kb.next();

//while the user input is invalid ask them again
while(!response.equals("yes") && !response.equals("no")){
System.out.println("Response must be yes or no");
System.out.println("Would you like to sort again? (yes/no)");
response=kb.next();
}
//if they choose not to, we are done
if(response.equals("no"))
break;
}
}
}
65 changes: 65 additions & 0 deletions Sorting Algorithm/insertinSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.util.Scanner;
class insertinSort{

public static void insertionSort(int[]arr){
//loop over array
for(int i=1; i<arr.length; i++){
int j=i;
/*while the ith element has not been swapped to its correct position
(with respect to the firt i elements in the array)*/
while(j>0){
//if the element preceeding this element is larger
if(arr[j]<arr[j-1]){
//we swap the elements and continue to move our element down the list by decrementing its position
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
j--;
}
//otherwise, it is in the correct position so far and we break
else
break;
}
}
}

public static void main(String[]args){
//initialize scanner
Scanner kb=new Scanner(System.in);
//while user has not quit
while(true){
//prompt user for array size
System.out.println("Enter an array size");
//get length of new array
int len=kb.nextInt();
//make new array of length len
int[]arr=new int[len];
//prompt user for array values
System.out.println("Enter "+len+" values for your array");
//fill the array with input values
for(int i=0; i<len; i++)
arr[i]=kb.nextInt();
//run the sort (java objects are pass by reference)
insertionSort(arr);
for(int i=0; i<arr.length; i++)
if(i!=arr.length-1)
System.out.print(arr[i]+" ");
else
System.out.println(arr[i]);

//ask user to go again
System.out.println("Would you like to sort again? (yes/no)");
String response=kb.next();

//while the user input is invalid ask them again
while(!response.equals("yes") && !response.equals("no")){
System.out.println("Response must be yes or no");
System.out.println("Would you like to sort again? (yes/no)");
response=kb.next();
}
//if they choose not to, we are done
if(response.equals("no"))
break;
}
}
}