From a21174ad4d2937841ec6b9ed106c9f0fa0794687 Mon Sep 17 00:00:00 2001 From: JimmyFromRobotics <34200973+JimmyFromRobotics@users.noreply.github.com> Date: Sun, 11 Oct 2020 14:16:07 -0400 Subject: [PATCH 1/3] Add heap sort in java --- Sorting Algorithm/heapSort.java | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Sorting Algorithm/heapSort.java diff --git a/Sorting Algorithm/heapSort.java b/Sorting Algorithm/heapSort.java new file mode 100644 index 0000000..6054f88 --- /dev/null +++ b/Sorting Algorithm/heapSort.java @@ -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*2arr[max]) + max=pos*2; + if(pos*2+1arr[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; + } + } +} From 7de2485e99bdd259d180903dd8165db1e3a311f6 Mon Sep 17 00:00:00 2001 From: JimmyFromRobotics <34200973+JimmyFromRobotics@users.noreply.github.com> Date: Sun, 11 Oct 2020 14:18:50 -0400 Subject: [PATCH 2/3] Implement insertion sort in java --- Sorting Algorithm/insertinSort.java | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Sorting Algorithm/insertinSort.java diff --git a/Sorting Algorithm/insertinSort.java b/Sorting Algorithm/insertinSort.java new file mode 100644 index 0000000..d60ffa1 --- /dev/null +++ b/Sorting Algorithm/insertinSort.java @@ -0,0 +1,65 @@ +import java.util.Scanner; +class insertinSort{ + + public static void insertionSort(int[]arr){ + //loop over array + for(int i=1; i0){ + //if the element preceeding this element is larger + if(arr[j] Date: Sun, 11 Oct 2020 14:48:03 -0400 Subject: [PATCH 3/3] Create LongestCommonSubsequenceLength --- .../LongestCommonSubsequenceLength | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Dynamic Programming/LongestCommonSubsequenceLength diff --git a/Dynamic Programming/LongestCommonSubsequenceLength b/Dynamic Programming/LongestCommonSubsequenceLength new file mode 100644 index 0000000..33f54f1 --- /dev/null +++ b/Dynamic Programming/LongestCommonSubsequenceLength @@ -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