diff --git a/Arrays/binarysearch.class b/Arrays/binarysearch.class new file mode 100644 index 0000000..c9bd048 Binary files /dev/null and b/Arrays/binarysearch.class differ diff --git a/Arrays/binarysearch.java b/Arrays/binarysearch.java new file mode 100644 index 0000000..e321992 --- /dev/null +++ b/Arrays/binarysearch.java @@ -0,0 +1,25 @@ +import java.util.*; +public class binarysearch { + public static int bin(int numbers[],int key){ + int start=0,end = numbers.length-1; + + while(start<=end){ + int mid=(start+end)/2; + if(numbers[mid]==key){ + return mid; + } + if(numbers[mid]arr[j+1]){ + int temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + public static void printarr(int arr[]){ + for(int i=0;i0){ + arr[j]=i; + j++; + count[i]--; + } + + } + } + + public static void main(String args[]){ + int arr[] = {1,4,1,3,2,4,3,7}; + + count(arr); + printarr(arr); + } +} diff --git a/Arrays/eams.class b/Arrays/eams.class new file mode 100644 index 0000000..fec3f19 Binary files /dev/null and b/Arrays/eams.class differ diff --git a/Arrays/eams.java b/Arrays/eams.java new file mode 100644 index 0000000..19d7fcd --- /dev/null +++ b/Arrays/eams.java @@ -0,0 +1,16 @@ +import java.util.*; +public class eams { + public static void update(int marks[]){ + for( int i=0;i=0 && arr[prev]>curr){ + arr[prev+1]=arr[prev]; + prev--; + } + //insertion + arr[prev+1]=curr; + } +} + public static void main(String args[]){ + int arr[] = {1,4,1,3,2,4,3,7}; + insertion(arr); + printarr(arr); + } +} diff --git a/Arrays/jajmo.java b/Arrays/jajmo.java new file mode 100644 index 0000000..df98078 --- /dev/null +++ b/Arrays/jajmo.java @@ -0,0 +1,3 @@ +public class jajmo { + +} diff --git a/Arrays/kadanes.java b/Arrays/kadanes.java new file mode 100644 index 0000000..e133c1e --- /dev/null +++ b/Arrays/kadanes.java @@ -0,0 +1,21 @@ +public class kadanes { + public static void kad(int numbers[]) + { + int cs=0; + int ms=Integer.MIN_VALUE; + for(int i=0; iarr[j]){ + minpos=j; + } + } + int temp=arr[minpos]; + arr[minpos]=arr[i]; + arr[i]=temp; + } +} + public static void main(String args[]){ + int arr[] = {1,4,1,3,2,4,3,7}; + select(arr); + + printarr(arr); + } +} diff --git a/Arrays/sort.java b/Arrays/sort.java new file mode 100644 index 0000000..868765e --- /dev/null +++ b/Arrays/sort.java @@ -0,0 +1,75 @@ +public class sort { + public static void bub(int arr[]) + { + for(int turn=0;turnarr[j+1]){ + int temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + public static void printarr(int arr[]){ + for(int i=0;iarr[j]){ + minpos=j; + } + } + int temp=arr[minpos]; + arr[minpos]=arr[i]; + arr[i]=temp; + } +} +public static void insertion(int arr[]){ + for(int i=1;i=0 && arr[prev]>curr){ + arr[prev+1]=arr[prev]; + prev--; + } + //insertion + arr[prev+1]=curr; + + } +} +public static void count(int arr[]){ + int largest=Integer.MIN_VALUE; +for(int i=0;i0){ + arr[j]=i; + j++; + count[i]--; + } + + } +} + + public static void main(String args[]){ + int arr[] = {1,4,1,3,2,4,3,7}; + //count(arr); + //insertion(arr); + //select(arr); + bub(arr); + printarr(arr); + } +} diff --git a/Arrays/spiral.java b/Arrays/spiral.java new file mode 100644 index 0000000..fae4499 --- /dev/null +++ b/Arrays/spiral.java @@ -0,0 +1,48 @@ +public class spiral { + public static void printspiral(int matrix[][]){ + int startrow=0; + int endrow=matrix.length-1; + int startcol=0; + int endcol=matrix[0].length-1; + + while(startrow<=endrow && startcol<=endcol){ + //top + for(int j=startcol;j<=endcol;j++){ + System.out.print(matrix[startrow][j]+ " "); + } + + //right + for(int i=startrow+1;i<=endrow;i++){ + System.out.print(matrix[i][endcol]+ " "); + } + + //bottom + for(int j=endcol-1;j>=startcol;j--){ + if(startrow==endrow){ + break; + } + System.out.print(matrix[endrow][j] + " "); + } + + //left + for(int i=endrow-1;i>=startrow+1;i--){ + if(startcol==endcol){ + break; + } + System.out.print(matrix[i][startcol]+ " "); + } + startcol++; + startrow++; + endrow--; + endcol--; + } + System.out.println(); + } + public static void main(String args[]){ + int matrix[][]={{1,2,3,4}, + {5,6,7,8}, + {9,10,11,12}, + {13,14,15,16}}; + printspiral(matrix); + } +} diff --git a/Arrays/spiralmatrix.java b/Arrays/spiralmatrix.java new file mode 100644 index 0000000..2ff06f7 --- /dev/null +++ b/Arrays/spiralmatrix.java @@ -0,0 +1,48 @@ +public class spiralmatrix { + public static void printspiral(int matrix[][]){ + int startrow=0; + int endrow=matrix.length-1; + int startcol=0; + int endcol=matrix[0].length-1; + + while(startrow<=endrow && startcol<=endcol){ + //top + for(int j=startcol;j<=endcol;j++){ + System.out.print(matrix[startrow][j]+ " "); + } + + //right + for(int i=startrow+1;i<=endrow;i++){ + System.out.print(matrix[i][endcol]+ " "); + } + + //bottom + for(int j=endcol-1;j>=startcol;j--){ + if(startrow==endrow){ + break; + } + System.out.print(matrix[endrow][j] + " "); + } + + //left + for(int i=endrow-1;i>=startrow+1;i--){ + if(startcol==endcol){ + break; + } + System.out.print(matrix[i][startcol]+ " "); + } + startcol++; + startrow++; + endrow--; + endcol--; + } + System.out.println(); + } + public static void main(string args[]){ + int matrix[][]={{1,2,3,4}, + {5,6,7,8}, + {9,10,11,12}, + {13,14,15,16}}; + printspiral(matrix); + } +} diff --git a/Arrays/stocks.class b/Arrays/stocks.class new file mode 100644 index 0000000..1bc9dd9 Binary files /dev/null and b/Arrays/stocks.class differ diff --git a/Arrays/stocks.java b/Arrays/stocks.java new file mode 100644 index 0000000..04c9b95 --- /dev/null +++ b/Arrays/stocks.java @@ -0,0 +1,23 @@ +public class stocks { + public static int buyandsellpricestocks(int prices[]) + { + int buyprice=Integer.MAX_VALUE; + int maxprofit=0; + for(int i=0; i=0;i--){ + rightmax[i]=Math.max(height[i],leftmax[i+1]); + } + int trappedwat=0; + + //loop + for(int i=0;i