From db18614c5566e20b768dea60a49ee82d162aef01 Mon Sep 17 00:00:00 2001 From: suraj-goel Date: Tue, 2 Oct 2018 09:16:30 +0530 Subject: [PATCH 1/6] Add dp programs --- dynamic-programming/EDIST.java | 46 +++++++++++++++++++++++++++++++ dynamic-programming/Knapsack.java | 42 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 dynamic-programming/EDIST.java create mode 100644 dynamic-programming/Knapsack.java diff --git a/dynamic-programming/EDIST.java b/dynamic-programming/EDIST.java new file mode 100644 index 0000000..6c3c7dc --- /dev/null +++ b/dynamic-programming/EDIST.java @@ -0,0 +1,46 @@ + +// A Naive recursive Java program to find minimum number +// operations to convert str1 to str2 +class EDIST +{ + static int min(int x,int y,int z) + { + if (x<=y && x<=z) return x; + if (y<=x && y<=z) return y; + else return z; + } + + static int editDist(String str1 , String str2 , int m ,int n) + { + // If first string is empty, the only option is to + // insert all characters of second string into first + if (m == 0) return n; + + // If second string is empty, the only option is to + // remove all characters of first string + if (n == 0) return m; + + // If last characters of two strings are same, nothing + // much to do. Ignore last characters and get count for + // remaining strings. + if (str1.charAt(m-1) == str2.charAt(n-1)) + return editDist(str1, str2, m-1, n-1); + + // If last characters are not same, consider all three + // operations on last character of first string, recursively + // compute minimum cost for all three operations and take + // minimum of three values. + return 1 + min ( editDist(str1, str2, m, n-1), // Insert + editDist(str1, str2, m-1, n), // Remove + editDist(str1, str2, m-1, n-1) // Replace + ); + } + + public static void main(String args[]) + { + String str1 = "sunday"; + String str2 = "saturday"; + + System.out.println( editDist( str1 , str2 , str1.length(), str2.length()) ); + } +} \ No newline at end of file diff --git a/dynamic-programming/Knapsack.java b/dynamic-programming/Knapsack.java new file mode 100644 index 0000000..0172762 --- /dev/null +++ b/dynamic-programming/Knapsack.java @@ -0,0 +1,42 @@ + +// A Dynamic Programming based solution for 0-1 Knapsack problem +class Knapsack +{ + + // A utility function that returns maximum of two integers + static int max(int a, int b) { return (a > b)? a : b; } + + // Returns the maximum value that can be put in a knapsack of capacity W + static int knapSack(int W, int wt[], int val[], int n) + { + int i, w; + int K[][] = new int[n+1][W+1]; + + // Build table K[][] in bottom up manner + for (i = 0; i <= n; i++) + { + for (w = 0; w <= W; w++) + { + if (i==0 || w==0) + K[i][w] = 0; + else if (wt[i-1] <= w) + K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); + else + K[i][w] = K[i-1][w]; + } + } + + return K[n][W]; + } + + + // Driver program to test above function + public static void main(String args[]) + { + int val[] = new int[]{60, 100, 120}; + int wt[] = new int[]{10, 20, 30}; + int W = 50; + int n = val.length; + System.out.println(knapSack(W, wt, val, n)); + } +} \ No newline at end of file From d300f1de8f7b8cb3b0aa7093b0d29dc07911cbc7 Mon Sep 17 00:00:00 2001 From: Anwar Rafiq shaikh Date: Tue, 2 Oct 2018 09:37:18 +0530 Subject: [PATCH 2/6] SegmenTree.java I have created a Segment tree, to find maximum element in given range. --- SegmentTree.java | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 SegmentTree.java diff --git a/SegmentTree.java b/SegmentTree.java new file mode 100644 index 0000000..0fb2f83 --- /dev/null +++ b/SegmentTree.java @@ -0,0 +1,101 @@ +import java.util.*; +import java.lang.*; +import java.io.*; +class SegmentTree +{ + int st[]; + Segment(int []arr, int n) + { + int x = (int) Math.ceil((Math.log(n)/Math.log(2))); + int max_size = 2 *(int) (Math.pow(2,x) - 1); + st = new int[max_size]; + + constSegmentUtil(arr,0,n-1,0); + } + + int constSegmentUtil(int []arr, int ss,int se,int si) + { + if(ss == se) + { + st[si] = arr[ss]; + return arr[ss]; + } + int mid = getMid(ss,se); + st[si] = max(constSegmentUtil(arr,ss,mid,2*si+1),constSegmentUtil(arr,mid+1,se,2*si+2)); + return st[si]; + } + + int getMid(int s,int e) + { + return s + (e - s)/2; + } + + int max(int a,int b) + { + if(a > b) + { + return a; + } + else{ + return b; + } + } + + void print(){ + for (int i=0;i=se) + { + return st[si]; + } + + if(seqe) + { + return 0; + } + + int mid = getMid(ss,se); + + return max(getMaxUtil(ss,mid,qs,qe,2*si+1),getMaxUtil(mid+1,se,qs,qe,2*si+2)); + } + + + int getMax(int n,int qs,int qe){ + + if(qs > qe || qs < 0 || qe > n-1){ + return -1; + } + + return getMaxUtil(0,n-1,qs,qe,0); + } + + public static void main (String[] args) throws java.lang.Exception + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine());//for testcase + String []temp = br.readLine().split(" ");//taking array as string + int []arr = new int[t]; + for(int i=0;i Date: Tue, 2 Oct 2018 00:36:35 -0400 Subject: [PATCH 3/6] add link to algorithms on readme --- README.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 25f9edd..2f64226 100644 --- a/README.md +++ b/README.md @@ -16,19 +16,21 @@ ## Contents -- [Arithmetic Analysis]() -- [File Transfer Protocol]() -- [Graphs]() -- [Math]() -- [Neutral Network]() -- [Ciphers]() -- [Data Structures]() -- [Dynamic Programming]() -- [Hashes]() -- [Searches]() -- [Sorting]() -- [Strings]() -- [Traversals]() +- [Arithmetic Analysis](arithmetic-analysis) +- [File Transfer Protocol](file-transfer-protocol) +- [Greedy Algorithms](greedy-algorithms) +- [Graphs](graphs) +- [Math](math) +- [Neutral Network](neutral-network) +- [Ciphers](ciphers) +- [Data Structures](data-structures) +- [Dynamic Programming](dynamic-programming) +- [Hashes](hashes) +- [Searches](searches) +- [Sorting](sorting) +- [Strings](strings) +- [Traversals](traversals) + ## License From 55d8f193ed10a04638d892ecfb96aee538073b67 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Tue, 2 Oct 2018 00:39:44 -0400 Subject: [PATCH 4/6] Rename SegmentTree.java to data-structures/SegmentTree.java --- SegmentTree.java => data-structures/SegmentTree.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename SegmentTree.java => data-structures/SegmentTree.java (100%) diff --git a/SegmentTree.java b/data-structures/SegmentTree.java similarity index 100% rename from SegmentTree.java rename to data-structures/SegmentTree.java From 5cddf1073ba4db21f1cd9751f404da6330d9f9cf Mon Sep 17 00:00:00 2001 From: Yash Agarwal Date: Tue, 2 Oct 2018 21:02:59 +0530 Subject: [PATCH 5/6] Added QuickSort --- sorting/QuickSort.java | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 sorting/QuickSort.java diff --git a/sorting/QuickSort.java b/sorting/QuickSort.java new file mode 100644 index 0000000..6574b38 --- /dev/null +++ b/sorting/QuickSort.java @@ -0,0 +1,78 @@ +// Java program for implementation of QuickSort +class QuickSort +{ + /* This function takes last element as pivot, + places the pivot element at its correct + position in sorted array, and places all + smaller (smaller than pivot) to left of + pivot and all greater elements to right + of pivot */ + int partition(int arr[], int low, int high) + { + int pivot = arr[high]; + int i = (low-1); // index of smaller element + for (int j=low; j Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i Date: Tue, 2 Oct 2018 21:06:51 +0530 Subject: [PATCH 6/6] Added ShellSort --- sorting/ShellSort.java | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sorting/ShellSort.java diff --git a/sorting/ShellSort.java b/sorting/ShellSort.java new file mode 100644 index 0000000..2be3987 --- /dev/null +++ b/sorting/ShellSort.java @@ -0,0 +1,58 @@ +class ShellSort +{ + /* An utility function to print array of size n*/ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i 0; gap /= 2) + { + // Do a gapped insertion sort for this gap size. + // The first gap elements a[0..gap-1] are already + // in gapped order keep adding one more element + // until the entire array is gap sorted + for (int i = gap; i < n; i += 1) + { + // add a[i] to the elements that have been gap + // sorted save a[i] in temp and make a hole at + // position i + int temp = arr[i]; + + // shift earlier gap-sorted elements up until + // the correct location for a[i] is found + int j; + for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) + arr[j] = arr[j - gap]; + + // put temp (the original a[i]) in its correct + // location + arr[j] = temp; + } + } + return 0; + } + + // Driver method + public static void main(String args[]) + { + int arr[] = {12, 34, 54, 2, 3}; + System.out.println("Array before sorting"); + printArray(arr); + + ShellSort ob = new ShellSort(); + ob.sort(arr); + + System.out.println("Array after sorting"); + printArray(arr); + } +} \ No newline at end of file