diff --git a/Bubble.class b/Bubble.class new file mode 100644 index 0000000..9e936b2 Binary files /dev/null and b/Bubble.class differ diff --git a/Bubble.java b/Bubble.java new file mode 100644 index 0000000..d5b4a04 --- /dev/null +++ b/Bubble.java @@ -0,0 +1,46 @@ +import java.util.Scanner; +class Bubble +{ + void bubblesort(int arr[]) + { + int i,j,temp; + boolean swapped=false; + int n=arr.length; + for(i=0;iarr[j+1]) + { + temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + swapped=true; + } + } + if(swapped==false) + { + break; + } + } + } + public static void main(String args[]) + { + Bubble b=new Bubble(); + Scanner sc=new Scanner(System.in); + int arr[]=new int[10]; + for(int i=0;i<10;i++) + { + arr[i]=sc.nextInt(); + } + b.bubblesort(arr); + System.out.println("Sorted array"); + for(int i=0;i=7) + { + d3=calcdist(arr,mid-2,mid+5); + } + int d=Math.min(dl,dr); + int val=Math.min(d,d3); + return val; + } + public static void main(String args[]) + { + + int arr[]={13,15,12,7,14,16,1,10,17,9,2,11,23,18,24,5,0,6}; + int c=closest(arr); + System.out.println("Distance between closest pair of points="+c); + } +} \ No newline at end of file diff --git a/DivideNConquer/Duplicates.class b/DivideNConquer/Duplicates.class new file mode 100644 index 0000000..ac0b112 Binary files /dev/null and b/DivideNConquer/Duplicates.class differ diff --git a/DivideNConquer/Duplicates.java b/DivideNConquer/Duplicates.java new file mode 100644 index 0000000..7fa49aa --- /dev/null +++ b/DivideNConquer/Duplicates.java @@ -0,0 +1,103 @@ +import java.io.*; +import java.util.*; +class Duplicates +{ + static void sort(int arr[]) + { + int n=arr.length; + for(int i=1;i=0 && arr[j]>key) + { + //move elements greater than key to one space ahead to make space for key + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=key; + } + } + public static int first(int arr[], int low, int high, int x, int n) + { + if (high >= low) { + int mid = low + (high - low) / 2; + if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x) + return mid; + else if (x > arr[mid]) + return first(arr, (mid + 1), high, x, n); + else + return first(arr, low, (mid - 1), x, n); + } + return -1; + } + public static int last(int arr[],int low,int high,int x,int n) + { + if(high>=low) + { + int mid=low+(high-low)/2; + if((mid==n-1||xx) + { + return last(arr,low,mid-1,x,n); + } + else + { + return last(arr,mid+1,high,x,n); + } + + } + return -1; + } + public static void main(String args[]) + { + try + { + /*FileReader fr=new FileReader("input.txt"); + int ch; + int arr[]=new int[100]; + int i=0; + int d=0;int num; + while((ch=fr.read())!=-1) + { + num=0; + while((char)ch!=',') + { + d=Character.getNumericValue((char)ch); + num=(num*10)+d; + ch=fr.read(); + + } + arr[i]=num; + i++; + } + if(ch==-1) + { + fr.close();*/ + int arr[]={1,1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,10,11,20,23,23,23,23,67,67,67,67,89,101,105,106,106,106,106,106,203,245,245,245,367,8,8,8,8,34,34,52,1067,2345,435,567,567,890,780,657,123}; + Scanner sc=new Scanner(System.in); + int i=arr.length; + sort(arr); + int x=sc.nextInt(); + int f=first(arr, 0, i - 1, x, i); + int l=last(arr, 0, i - 1, x, i); + int c=0; + for(int j=f;j<=l;j++) + { + if(arr[j]==x) + { + c++; + } + } + System.out.println("Number of occurences= "+c); + //} + } + catch (Exception e) + { + System.out.println("Exception encountered"); + } + } +} \ No newline at end of file diff --git a/DivideNConquer/Dutch.class b/DivideNConquer/Dutch.class new file mode 100644 index 0000000..0e59167 Binary files /dev/null and b/DivideNConquer/Dutch.class differ diff --git a/DivideNConquer/Dutch.java b/DivideNConquer/Dutch.java new file mode 100644 index 0000000..fbd7cd8 --- /dev/null +++ b/DivideNConquer/Dutch.java @@ -0,0 +1,104 @@ +import java.io.*; + +class Pair { + private int x; + private int y; + + Pair(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { return x; } + public int getY() { return y; } +} + +class Dutch +{ + public static void swap (int[] arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + // Partition routine using Dutch National Flag Algorithm + public static Pair Partition(int[] arr, int start, int end) + { + int mid = start; + int pivot = arr[end]; + + while (mid <= end) { + if (arr[mid] < pivot) { + swap(arr, start, mid); + ++start; + ++mid; + } + else if (arr[mid] > pivot) { + swap(arr, mid, end); + --end; + } + else { + ++mid; + } + } + + // arr[start .. mid - 1] contains all occurrences of pivot + return new Pair(start - 1, mid); + } + + // Three-way Quicksort routine + public static void quicksort(int[] arr, int start, int end) + { + // base condition for 0 or 1 elements + if (start >= end) { + return; + } + + // handle 2 elements separately as Dutch national flag + // algorithm will work for 3 or more elements + if (start - end == 1) + { + if (arr[start] < arr[end]) { + swap(arr, start, end); + } + return; + } + + // rearrange the elements across pivot using Dutch + // national flag problem algorithm + Pair pivot = Partition(arr, start, end); + + // recur on sub-array containing elements that are less than pivot + quicksort(arr, start, pivot.getX()); + + // recur on sub-array containing elements that are more than pivot + quicksort(arr, pivot.getY(), end); + } + public static void main(String[] args)throws Exception + { + String arr[] = new String[100]; + File f=new File("num.txt"); + BufferedReader br = new BufferedReader(new FileReader(f)); + String st; + String s2=""; + int num[]=new int[100]; + while ((st = br.readLine()) != null) + { + s2=s2.concat(st); + } + String words[]=s2.split(","); + int n=words.length; + for(int i1=0;i1=0;j--) + { + minht=Math.min(arr[j],minht); + int width=(j-i+1); + maxarea=Math.max(maxarea,(minht*width)); + } + } + return maxarea; + } + public static void main(String args[]) + { + int arr[]={1,2,3,3,4,5,6,5,4}; + int a=area(arr); + System.out.println("Area"+a); + + } +} \ No newline at end of file diff --git a/DivideNConquer/Hoare.class b/DivideNConquer/Hoare.class new file mode 100644 index 0000000..4480a5b Binary files /dev/null and b/DivideNConquer/Hoare.class differ diff --git a/DivideNConquer/Hoare.java b/DivideNConquer/Hoare.java new file mode 100644 index 0000000..c20d5b9 --- /dev/null +++ b/DivideNConquer/Hoare.java @@ -0,0 +1,62 @@ +import java.io.*; +class Hoare +{ + static int partition(int arr[],int lo,int hi) + { + int pivot = arr[lo]; + int i=lo-1,j=hi+1; + while(true) + { + do + { + i++; + }while(arr[i]pivot); + + if (i >= j) + return j; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + static void quicksort(int []arr, int low, + int high) + { + if (low < high) + { + int pi = partition(arr, low, high); + quicksort(arr, low, pi); + quicksort(arr, pi + 1, high); + } + } + public static void main(String[] args)throws Exception + { + String arr[] = new String[100]; + File f=new File("num.txt"); + BufferedReader br = new BufferedReader(new FileReader(f)); + String st; + String s2=""; + int num[]=new int[100]; + while ((st = br.readLine()) != null) + { + s2=s2.concat(st); + } + String words[]=s2.split(","); + int n=words.length; + for(int i1=0;i1 arr[high]) + { + p.max = arr[low]; + p.min = arr[high]; + } else + { + p.max = arr[high]; + p.min = arr[low]; + } + return p; + } + mid = (low + high) / 2; + mml = getMinMax(arr, low, mid); + mmr = getMinMax(arr, mid + 1, high); + + if (mml.min < mmr.min) + { + p.min = mml.min; + } + else + { + p.min = mmr.min; + } + if (mml.max > mmr.max) + { + p.max = mml.max; + } else + { + p.max = mmr.max; + } + return p; + } + + public static void main(String args[]) + { + int arr[] = {1000, 11, 445, 1, 330, 3000}; + int arr_size = 6; + Pair minmax = getMinMax(arr, 0, arr_size - 1); + System.out.printf("\nMinimum element is %d", minmax.min); + System.out.printf("\nMaximum element is %d", minmax.max); + } +} \ No newline at end of file diff --git a/DivideNConquer/Missing.class b/DivideNConquer/Missing.class new file mode 100644 index 0000000..ad40ee9 Binary files /dev/null and b/DivideNConquer/Missing.class differ diff --git a/DivideNConquer/Missing.java b/DivideNConquer/Missing.java new file mode 100644 index 0000000..136c26f --- /dev/null +++ b/DivideNConquer/Missing.java @@ -0,0 +1,88 @@ +import java.io.*; +import java.util.*; +class Missing +{ + static void sort(int arr[]) + { + int n=arr.length; + for(int i=1;i=0 && arr[j]>key) + { + //move elements greater than key to one space ahead to make space for key + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=key; + } + } + static int search(int arr[],int l,int r,int x) + { + if(r>=l) + { + int m=l+(r-l)/2; + if(arr[m]==x) + { + return m; + } + else if(arr[m]x) + { + return search(arr,l,m-1,x); + } + } + return -1; + } + public static void main(String args[]) + { + try + { + /*FileReader fr=new FileReader("input.txt"); + int ch; + int arr[]=new int[100]; + int i=0; + int d=0;int num; + while((ch=fr.read())!=-1) + { + num=0; + while((char)ch!=',') + { + d=Character.getNumericValue((char)ch); + num=(num*10)+d; + ch=fr.read(); + + } + arr[i]=num; + i++; + } + if(ch==-1) + { + fr.close();*/ + int arr[]={1,1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,10,11,20,23,23,23,23,67,67,67,67,89,101,105,106,106,106,106,106,203,245,245,245,367,8,8,8,8,34,34,52,1067,2345,435,567,567,890,780,657,123}; + Scanner sc=new Scanner(System.in); + int i=arr.length; + sort(arr); + //int x=sc.nextInt(); + int max=arr[i-1]; + for(int j=1;j=0 && arr[j]>key) + { + //move elements greater than key to one space ahead to make space for key + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=key; + } + } + public static int first(int arr[], int low, int high, int x, int n) + { + if (high >= low) { + int mid = low + (high - low) / 2; + if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x) + return mid; + else if (x > arr[mid]) + return first(arr, (mid + 1), high, x, n); + else + return first(arr, low, (mid - 1), x, n); + } + return -1; + } + public static int last(int arr[],int low,int high,int x,int n) + { + if(high>=low) + { + int mid=low+(high-low)/2; + if((mid==n-1||xx) + { + return last(arr,low,mid-1,x,n); + } + else + { + return last(arr,mid+1,high,x,n); + } + + } + return -1; + } + public static void main(String args[]) + { + try + { + /*FileReader fr=new FileReader("input.txt"); + int ch; + int arr[]=new int[100]; + int i=0; + int d=0;int num; + while((ch=fr.read())!=-1) + { + num=0; + while((char)ch!=',') + { + d=Character.getNumericValue((char)ch); + num=(num*10)+d; + ch=fr.read(); + + } + arr[i]=num; + i++; + } + if(ch==-1) + { + fr.close();*/ + int arr[]={0,1,1,1,0,1,0,0,1,0,0,1,0,1}; + Scanner sc=new Scanner(System.in); + int i=arr.length; + sort(arr); + int x=1; + int f=first(arr, 0, i - 1, x, i); + int l=last(arr, 0, i - 1, x, i); + int c=0; + for(int j=f;j<=l;j++) + { + if(arr[j]==x) + { + c++; + } + } + System.out.println("Number of occurences= "+c); + //} + } + catch (Exception e) + { + System.out.println("Exception encountered"); + } + } +} \ No newline at end of file diff --git a/DivideNConquer/Occurence.class b/DivideNConquer/Occurence.class new file mode 100644 index 0000000..39af76c Binary files /dev/null and b/DivideNConquer/Occurence.class differ diff --git a/DivideNConquer/Occurence.java b/DivideNConquer/Occurence.java new file mode 100644 index 0000000..7718605 --- /dev/null +++ b/DivideNConquer/Occurence.java @@ -0,0 +1,94 @@ +import java.io.*; +import java.util.*; +class Occurence +{ + static void sort(int arr[]) + { + int n=arr.length; + for(int i=1;i=0 && arr[j]>key) + { + //move elements greater than key to one space ahead to make space for key + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=key; + } + } + public static int first(int arr[], int low, int high, int x, int n) + { + if (high >= low) { + int mid = low + (high - low) / 2; + if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x) + return mid; + else if (x > arr[mid]) + return first(arr, (mid + 1), high, x, n); + else + return first(arr, low, (mid - 1), x, n); + } + return -1; + } + public static int last(int arr[],int low,int high,int x,int n) + { + if(high>=low) + { + int mid=low+(high-low)/2; + if((mid==n-1||xx) + { + return last(arr,low,mid-1,x,n); + } + else + { + return last(arr,mid+1,high,x,n); + } + + } + return -1; + } + public static void main(String args[]) + { + try + { + /*FileReader fr=new FileReader("input.txt"); + int ch; + int arr[]=new int[100]; + int i=0; + int d=0;int num; + while((ch=fr.read())!=-1) + { + num=0; + while((char)ch!=',') + { + d=Character.getNumericValue((char)ch); + num=(num*10)+d; + ch=fr.read(); + + } + arr[i]=num; + i++; + } + if(ch==-1) + { + fr.close();*/ + int arr[]={1,1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,10,11,20,23,23,23,23,67,67,67,67,89,101,105,106,106,106,106,106,203,245,245,245,367,8,8,8,8,34,34,52,1067,2345,435,567,567,890,780,657,123}; + Scanner sc=new Scanner(System.in); + int i=arr.length; + sort(arr); + int x=sc.nextInt(); + System.out.println("First Occurrence = " + first(arr, 0, i - 1, x, i)); + System.out.println("Last Occurrence = " + last(arr, 0, i - 1, x, i)); + //} + } + catch (Exception e) + { + System.out.println("Exception encountered"); + } + } +} \ No newline at end of file diff --git a/DivideNConquer/Pair.class b/DivideNConquer/Pair.class new file mode 100644 index 0000000..b52c446 Binary files /dev/null and b/DivideNConquer/Pair.class differ diff --git a/DivideNConquer/Peak.class b/DivideNConquer/Peak.class new file mode 100644 index 0000000..eecd46c Binary files /dev/null and b/DivideNConquer/Peak.class differ diff --git a/DivideNConquer/Peak.java b/DivideNConquer/Peak.java new file mode 100644 index 0000000..f35b5c3 --- /dev/null +++ b/DivideNConquer/Peak.java @@ -0,0 +1,25 @@ +import java.util.*; +class Peak +{ + static int findPeak(int arr[],int l,int r,int n) + { + int mid=l+(r-l)/2; + if((mid==0||arr[mid-1]0 && arr[mid-1]>arr[mid]) + { + return findPeak(arr,l,mid-1,n); + } + else + return findPeak(arr,mid+1,r,n); + } + public static void main(String[] args) + { + int arr[] = { 10, 20, 15, 2, 23, 90, 67}; + int n = arr.length; + System.out.println( + "Index of a peak point is " + findPeak(arr, 0,n-1,n)); + } +} \ No newline at end of file diff --git a/DivideNConquer/PowerOfN.class b/DivideNConquer/PowerOfN.class new file mode 100644 index 0000000..a5283c6 Binary files /dev/null and b/DivideNConquer/PowerOfN.class differ diff --git a/DivideNConquer/PowerOfN.java b/DivideNConquer/PowerOfN.java new file mode 100644 index 0000000..ef0df57 --- /dev/null +++ b/DivideNConquer/PowerOfN.java @@ -0,0 +1,27 @@ +import java.util.*; +class PowerOfN +{ + int powpow(int x,int y) + { + if(y==0) + { + return 1; + } + else if(y%2==0) + { + return powpow(x,y/2)*powpow(x,y/2); + } + else + { + return x*powpow(x,y/2)*powpow(x,y/2); + } + } + public static void main(String args[]) + { + Scanner sc=new Scanner(System.in); + PowerOfN p=new PowerOfN(); + int x=sc.nextInt(); + int y=sc.nextInt(); + System.out.println("Power of n="+p.powpow(x,y)); + } +} \ No newline at end of file diff --git a/DivideNConquer/Prefix.class b/DivideNConquer/Prefix.class new file mode 100644 index 0000000..d39a2e6 Binary files /dev/null and b/DivideNConquer/Prefix.class differ diff --git a/DivideNConquer/Prefix.java b/DivideNConquer/Prefix.java new file mode 100644 index 0000000..72e2d11 --- /dev/null +++ b/DivideNConquer/Prefix.java @@ -0,0 +1,55 @@ +import java.io.*; +class Prefix +{ + static String commonPrefix2(String s1,String s2) + { + String res=""; + int n1=s1.length(); + int n2=s2.length(); + for(int i=0,j=0;i<=n1-1 && j<=n2-1;i++,j++) + { + if(s1.charAt(i)!=s2.charAt(j)) + { + break; + } + res=res+s1.charAt(i); + } + return res; + } + static String commonPrefix(String arr[],int low,int high) + { + if(low==high) + { + return arr[low]; + } + if(high>low) + { + int mid=low+(high-low)/2; + String str1=commonPrefix(arr,low,mid); + String str2=commonPrefix(arr,mid+1,high); + return commonPrefix2(str1,str2); + } + return null; + } + public static void main(String[] args)throws Exception{ + String arr[] = new String[100]; + File f=new File("input.txt"); + BufferedReader br = new BufferedReader(new FileReader(f)); + String st; + String s2=""; + while ((st = br.readLine()) != null) + { + s2=s2.concat(st); + } + String words[]=s2.split(" "); + int n=words.length; + String ans = commonPrefix(words, 0, n - 1); + + if (ans.length() != 0) { + System.out.println("The longest common prefix is " + + ans); + } else { + System.out.println("There is no common prefix"); + } + } +} \ No newline at end of file diff --git a/DivideNConquer/SRoot.class b/DivideNConquer/SRoot.class new file mode 100644 index 0000000..5b5eeef Binary files /dev/null and b/DivideNConquer/SRoot.class differ diff --git a/DivideNConquer/SRoot.java b/DivideNConquer/SRoot.java new file mode 100644 index 0000000..3d45b59 --- /dev/null +++ b/DivideNConquer/SRoot.java @@ -0,0 +1,40 @@ +import java.util.*; +class SRoot +{ + static int root(int x) + { + if(x<=1) + { + return x; + } + else + { + int start=0,end=x,ans=0; + while(end>=start) + { + int mid=start+(end-start)/2; + if(mid*mid==x) + { + return mid; + } + else if(mid*mid= l; i--) + { + sum = sum + arr[i]; + if (sum > left_sum) + left_sum = sum; + } //max sum from mid to left + sum = 0; + int right_sum = Integer.MIN_VALUE; + for (int i = m + 1; i <= h; i++) + { + sum = sum + arr[i]; + if (sum > right_sum) + right_sum = sum; + } + sum=left_sum+right_sum; + return Math.max(sum,Math.max(left_sum, right_sum)); + } + static int maxSubArraySum(int arr[], int l,int h) + { + if(l==h) + { + return arr[l]; + } + int m=(l+h)/2; + return Math.max(Math.max(maxSubArraySum(arr, l, m), + maxSubArraySum(arr, m+1, h)), + maxCrossingSum(arr, l, m, h)); + } + public static void main(String[] args) + { + int arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}; + int n = arr.length; + int max_sum = maxSubArraySum(arr, 0, n-1); + + System.out.println("Maximum contiguous sum is "+ + max_sum); + } +} \ No newline at end of file diff --git a/DivideNConquer/input.txt b/DivideNConquer/input.txt new file mode 100644 index 0000000..57ec0f3 --- /dev/null +++ b/DivideNConquer/input.txt @@ -0,0 +1,2 @@ +appraisal appearance approach apple application append appendages appendix +apple appointed appreciate apply appease \ No newline at end of file diff --git a/DivideNConquer/num.txt b/DivideNConquer/num.txt new file mode 100644 index 0000000..f3d3d23 --- /dev/null +++ b/DivideNConquer/num.txt @@ -0,0 +1,2 @@ +1,4,67,89,42,35,46,78,98,134,234,567,789,234,9000,568,555,10,7,15,32, +52,64,31,97,160,543,239 \ No newline at end of file diff --git a/Insertion.class b/Insertion.class new file mode 100644 index 0000000..3542d7e Binary files /dev/null and b/Insertion.class differ diff --git a/Insertion.java b/Insertion.java new file mode 100644 index 0000000..e5154bb --- /dev/null +++ b/Insertion.java @@ -0,0 +1,31 @@ +class Insertion +{ + void sort(int arr[]) + { + int n=arr.length; + for(int i=1;i=0 && arr[j]>key) + { + //move elements greater than key to one space ahead to make space for key + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=key; + } + } + public static void main(String args[]) + { + int arr[]={38,27,43,3,9,82,10}; + int l=arr.length; + Insertion m=new Insertion(); + m.sort(arr); + System.out.println("Sorted array"); + for(int i=0;i