diff --git a/BinarySearch.java b/BinarySearch.java new file mode 100644 index 000000000000..2f19b426fd5b --- /dev/null +++ b/BinarySearch.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +class BinarySearch +{ + public static void main(String args[]) + { + int c, first, last, middle, n, search, array[]; + + Scanner in = new Scanner(System.in); + System.out.println("Enter number of elements"); + n = in.nextInt(); + array = new int[n]; + + System.out.println("Enter " + n + " integers"); + + + for (c = 0; c < n; c++) + array[c] = in.nextInt(); + + System.out.println("Enter value to find"); + search = in.nextInt(); + + first = 0; + last = n - 1; + middle = (first + last)/2; + + while( first <= last ) + { + if ( array[middle] < search ) + first = middle + 1; + else if ( array[middle] == search ) + { + System.out.println(search + " found at location " + (middle + 1) + "."); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if ( first > last ) + System.out.println(search + " is not present in the list.\n"); + } +} \ No newline at end of file diff --git a/BucketSort.java b/BucketSort.java new file mode 100644 index 000000000000..15d58f1b4ab0 --- /dev/null +++ b/BucketSort.java @@ -0,0 +1,19 @@ +import java.util.*; + +class BucketSort +{ + + public static void main(String arg[]) + { + Scanner sc=new Scanner(System.in); + System.out.println("Enter the size of array->"); + int n=sc.nextInt(); + double ar[]=new double[n]; + System.out.println("Enter "+n+" elements to be sort"); + for(int i=0;i"); + int n=sc.nextInt(); + int ar[]=new int[n]; + System.out.println("Enter the elements of array->"); + for(int i=0;i "); + for(int i=0;i "); + for(int i=0;ires){res=store[i];}} + System.out.println("Hence Maximum "+res+" colors will be required to color Graph"); }} \ No newline at end of file diff --git a/Greedy_activity_selection.java b/Greedy_activity_selection.java new file mode 100644 index 000000000000..5607e66ec4ae --- /dev/null +++ b/Greedy_activity_selection.java @@ -0,0 +1,27 @@ +import java.util.*; +public class Greedy_activity_selection +{ + static int solve(int ar[][]) + { + int max=1,lb=ar[0][1]; + for(int i=1;i=0 && ar[j]>k){ + ar[j+1]=ar[j]; + --j; + } + ar[j+1]=k; + } + long endTime = System.nanoTime(); + long totalTime = (endTime - startTime)/1000 ; + + System.out.println("\nSorted Array : "); + for(int i=0;ic[i-1][j]) + { + c[i][j]=v[i-1]+c[i-1][j-w[i-1]]; + } + else + { + c[i][j]=c[i-1][j]; + } + } + else + { + c[i][j]=c[i-1][j]; + } + } + } + + for(int i=0;i<=n;i++) + { + for(int j=0;j<=W;j++) + { + System.out.print(c[i][j]+"\t"); + } + System.out.println(); + } + System.out.println(); + System.out.println("The max value that can be put in knapsack capacity of "+W+" is "+c[n][W]); + } + + public static void main(String ar[]) + { + Scanner sc=new Scanner(System.in); + System.out.println("Enter number of items"); + int n=sc.nextInt(); + System.out.println("Enter capacity of knapsack"); + int W=sc.nextInt(); + int v[]=new int[n]; + int w[]=new int[n]; + System.out.println("Enter value array"); + for(int i=0;i=c[i][j-1]) + { + c[i][j]=c[i-1][j]; + b[i][j]='|'; + } + else if(c[i][j-1]>=c[i-1][j]) + { + c[i][j]=c[i][j-1]; + b[i][j]='-'; + } + } + } + System.out.println(); + System.out.println("Matrix for calculating size of LCS ==> "); + for(int i=0;i<=n1;i++) + { + for(int j=0;j<=n2;j++) + { + System.out.print(c[i][j]+" "); + } + System.out.println(); + } + System.out.println(); + System.out.println("Path Matrix for calculating LCS ==> "); + for(int i=0;i<=n1;i++) + { + for(int j=0;j<=n2;j++) + { + System.out.print(b[i][j]+" "); + } + System.out.println(); + } + System.out.println(); + System.out.print("Longest Common SubSequence is---> "); + new LCS().printLcs(b,input1,n1,n2); + } + + public void printLcs(char[][] b,char[] input1,int i,int j) + { + if(i==0 || j==0) + { + return; + } + if(b[i][j]=='\\') + { + printLcs(b,input1,i-1,j-1); + System.out.print(input1[i-1]+" "); + } + else if(b[i][j]=='|') + { + printLcs(b,input1,i-1,j); + } + else + printLcs(b,input1,i,j-1); + } +} + + + + + + + + + + +