diff --git a/CPP/Data Structure/binary_search.cpp b/CPP/Data Structure/binary_search.cpp new file mode 100644 index 0000000..9719c11 --- /dev/null +++ b/CPP/Data Structure/binary_search.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +int binarySearch(int arr[], int p, int r, int num) { + if (p <= r) { + int mid = (p + r)/2; + if (arr[mid] == num) + return mid ; + if (arr[mid] > num) + return binarySearch(arr, p, mid-1, num); + if (arr[mid] > num) + return binarySearch(arr, mid+1, r, num); + } + return -1; +} +int main(void) { + int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40}; + int n = sizeof(arr)/ sizeof(arr[0]); + int num = 33; + int index = binarySearch (arr, 0, n-1, num); + if(index == -1) + cout<< num <<" is not present in the array"; + else + cout<< num <<" is present at index "<< index <<" in the array"; + return 0; +} diff --git a/Java/Algorithms/BoyerMooreSearching.java b/Java/Algorithms/BoyerMooreSearching.java new file mode 100644 index 0000000..a38aa50 --- /dev/null +++ b/Java/Algorithms/BoyerMooreSearching.java @@ -0,0 +1,47 @@ +/* Java Program for Boyer Moore String Matching Algorithm */ + +import java.util.*; +import java.lang.*; +import java.io.*; + +class BoyerMooreSearching +{ + static int NO_OF_CHARS = 256; + static int badchar[] = new int[256]; + //The preprocessing done in constructor + BoyerMooreSearching( char []str) + { + int i, size = str.length; + for (i = 0; i < NO_OF_CHARS; i++) + badchar[i] = -1; + for (i = 0; i < size; i++) + badchar[(int) str[i]] = i; + } + + void search( char txt[], char pat[]) + { + int m = pat.length, n = txt.length, s = 0; + while(s <= (n - m)) + { + int j = m-1; + while(j >= 0 && pat[j] == txt[s+j]) j--; + if (j < 0) + { + System.out.println("Patterns found at " + s); + s += (s+m < n)? m-badchar[txt[s+m]] : 1; + } + else + s += Math.max(1, j - badchar[txt[s+j]]); + } + } + public static void main (String[] args) throws java.lang.Exception + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + char[] txt = br.readLine().toCharArray(); + char[] pat = br. readLine().toCharArray(); + BoyerMooreSearching obj = new BoyerMooreSearching(pat); + obj.search(txt, pat); + } +} + + \ No newline at end of file diff --git a/Java/Algorithms/KMP.java b/Java/Algorithms/KMP.java new file mode 100644 index 0000000..303c2b6 --- /dev/null +++ b/Java/Algorithms/KMP.java @@ -0,0 +1,61 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +class KMP +{ + static int lps[]; + void KMPSearch(String pat, String txt) + { + int M = pat.length(), N = txt.length(), j = 0, i = 0; + while (i < N) { + if (pat.charAt(j) == txt.charAt(i)) { + j++; + i++; + } + if (j == M) { + System.out.println("Found pattern " + + "at index " + (i - j)); + j = lps[j - 1]; + } + + else if (i < N && pat.charAt(j) != txt.charAt(i)) { + if (j != 0) + j = lps[j - 1]; + else + i = i + 1; + } + } + } + + KMP(String pat) + { + lps = new int[pat.length()]; + int len = 0, i = 1; + lps[0] = 0; + + while (i < pat.length()) { + if (pat.charAt(i) == pat.charAt(len)) { + len++; + lps[i] = len; + i++; + } + else + { + if (len != 0) len = lps[len - 1]; + else + { + lps[i] = len; + i++; + } + } + }} + + public static void main (String[] args) throws java.lang.Exception + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String txt = br.readLine(); + String pat = br.readLine(); + new KMP(pat).KMPSearch(pat, txt); + } +} diff --git a/Java/Algorithms/LowestCommonAncestor.java b/Java/Algorithms/LowestCommonAncestor.java new file mode 100644 index 0000000..7766129 --- /dev/null +++ b/Java/Algorithms/LowestCommonAncestor.java @@ -0,0 +1,56 @@ +class LowestCommonAncestor { + public static class TreeNode + { + int data; + TreeNode left; + TreeNode right; + TreeNode(int data) + { + this.data=data; + } + } + + public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode a, TreeNode b) { + if(root == null) + return null; + if(root.data == a.data || root.data == b.data ) + return root; + + TreeNode left=lowestCommonAncestor(root.left,a,b); + TreeNode right=lowestCommonAncestor(root.right,a,b); + + // If we get left and right not null , it is lca for a and b + if(left!=null && right!=null) + return root; + if(left== null) + return right; + else + return left; + } + + public static void main(String[] args) + { + //creating nodes + TreeNode rootNode =new TreeNode(40); + TreeNode node20=new TreeNode(20); + TreeNode node10=new TreeNode(10); + TreeNode node30=new TreeNode(30); + TreeNode node60=new TreeNode(60); + TreeNode node50=new TreeNode(50); + TreeNode node70=new TreeNode(70); + TreeNode node5=new TreeNode(5); + TreeNode node45=new TreeNode(45); + TreeNode node55=new TreeNode(55); + //linking nodes + rootNode.left=node20; + rootNode.right=node60; + node20.left=node10; + node20.right=node30; + node60.left=node50; + node60.right=node70; + node10.left=node5; + node50.right=node55; + + System.out.println("Lowest common ancestor for node 5 and 30: " + lowestCommonAncestor(rootNode,node5,node30).data); + } +} diff --git a/Java/Algorithms/MooreVoting.java b/Java/Algorithms/MooreVoting.java new file mode 100644 index 0000000..8711fc0 --- /dev/null +++ b/Java/Algorithms/MooreVoting.java @@ -0,0 +1,56 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +class MooreVoting +{ + public static void main (String[] args) throws java.lang.Exception + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int n = Integer.parseInt(br.readLine()); + int arr[] = new int[n]; + for(int i = 0; i < n; i++) + arr[i]=Integer.parseInt(br.readLine()); + int majority = majorityElement(arr); + if(majority>-1) + System.out.println("Majority Element: "+majority); + else + System.out.println("No majority element"); + } + + private static int majorityElement(int[] arr) { + int majority = -1; + int counter = 0; + int length = arr.length; + int index = 0; + while (index < length) { + if (counter == 0) { + majority = arr[index]; + counter++; + } else if (majority == arr[index]) { + counter++; + } else { + counter--; + } + index++; + } + + if (counter == 0) { + // No majority element found + return -1; + } + + index = -1; + counter = 0; + while (++index < length) { + if (majority == arr[index]) { + counter++; + } + } + + if (counter > length / 2) + return majority; + + return -1; + } +} diff --git a/Java/Algorithms/Trie.java b/Java/Algorithms/Trie.java new file mode 100644 index 0000000..e75fc99 --- /dev/null +++ b/Java/Algorithms/Trie.java @@ -0,0 +1,76 @@ +//Accepts only lowercase alphabets +class Trie { + static final int ALPHABET_SIZE = 26; + + static class TrieNode + { + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; + boolean isEndOfWord; + + TrieNode(){ + isEndOfWord = false; + for (int i = 0; i < ALPHABET_SIZE; i++) + children[i] = null; + } + }; + + static TrieNode root; + + static void insert(String key) + { + int level; + int length = key.length(); + int index; + TrieNode pCrawl = root; + for (level = 0; level < length; level++) + { + index = key.charAt(level) - 'a'; + if (pCrawl.children[index] == null) + pCrawl.children[index] = new TrieNode(); + pCrawl = pCrawl.children[index]; + } + pCrawl.isEndOfWord = true; + } + + static boolean search(String key) + { + int level; + int length = key.length(); + int index; + TrieNode pCrawl = root; + for (level = 0; level < length; level++) + { + index = key.charAt(level) - 'a'; + if (pCrawl.children[index] == null) + return false; + pCrawl = pCrawl.children[index]; + } + return (pCrawl != null && pCrawl.isEndOfWord); + } + + public static void main(String args[]) + { + String keys[] = {"the", "a", "there", "answer", "any", + "by", "bye", "their"}; + root = new TrieNode(); + int i; + for (i = 0; i < keys.length ; i++) + insert(keys[i]); + if(search("the") == true) + System.out.println("the --- Present in trie"); + else System.out.println("the --- Not present in trie"); + + if(search("these") == true) + System.out.println("these --- Present in trie"); + else System.out.println("these --- Not present in trie"); + + if(search("their") == true) + System.out.println("their --- Present in trie"); + else System.out.println("their --- Not present in trie"); + + if(search("thaw") == true) + System.out.println("thaw --- Present in trie"); + else System.out.println("thaw --- Not present in trie"); + + } +}