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 diff --git a/data-structures/SegmentTree.java b/data-structures/SegmentTree.java new file mode 100644 index 0000000..0fb2f83 --- /dev/null +++ b/data-structures/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 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 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 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy