Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BinarySearch Implementation in java and readme file #148

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
76 changes: 76 additions & 0 deletions Dynamic programming - algos/Kadane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.Scanner;

public class KadaneAlgo {
/* Kadane's algo is used to find maximum sum of subarray.
* here we keep track of all positive segments in array using current sum
* and we store the best positive segment sum in bestSoFar variable..
*
* Naive way of doing this i.e. finding all the subarrays first and then finding sum of all
* and then finding best out of it will take O(n^2) time and that is completely not accepted
* when we can do the same thing in O(n) using kadane algo.
* IN {-2,-3,4,-1,-2,1,5,-10} this example
* answer is {4,-1,-2,1,5) i.e. 7 .
*
* Example :
* INPUT:
* Enter the size of array
* 8
* Enter the element 1:
* -2
* Enter the element 2:
* -3
* Enter the element 3:
* 4
* Enter the element 4:
* -1
* Enter the element 5:
* -2
* Enter the element 6:
* 5
* Enter the element 7:
* 1
* Enter the element 8:
* -10
*
* OUTPUT:
* 7
*/
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter the size of array");
int n = s.nextInt();
int arr[] = new int[n];
for(int i=1;i<=n;i++){
System.out.println("Enter the element " + i + ":");
arr[i-1] = s.nextInt();
}
System.out.println(kadane(arr));
s.close();
}

public static int kadane(int arr[]){
int n= arr.length;
int currentMaxEnding = 0;
// assigning best sum minimum value so that it can be
// updated with any current max ending sum for the first time.
int bestSoFar =Integer.MIN_VALUE;
for(int i=0;i<n;i++){
// adding array elements to the currentMax ending sum..
currentMaxEnding+= arr[i];

// if current max ending gets greater than best sum we will update our best..
if(bestSoFar<currentMaxEnding)
bestSoFar = currentMaxEnding;

// if current sum has become -ve don't take that baggage
// for the next one.So we will make it 0.
if(currentMaxEnding<0){
currentMaxEnding = 0;
}
}


return bestSoFar;
}

}
56 changes: 56 additions & 0 deletions Dynamic programming - algos/LIS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.Scanner;

public class LIS{
/* In this problem we have to find length of longest increasing subsequence such that
* all elements of a subsequence are sorted in increasing order.
* for example : {40, 10, 20, 15, 50, 80} LIS is 4
* i.e {10,20,50,80}
*/
public static void main(String[] args) {
Scanner s= new Scanner(System.in);

System.out.println("Please enter the size of array");
int n= s.nextInt();
// declaring n sized array..
int arr[] = new int[n];
// taking array input..
for(int i=1;i<=n;i++){
System.out.println("Please enter element "+i +":");
arr[i-1]= s.nextInt();
}
// lenLongestIncSubs() will return length of increasing subsequence..
System.out.println(lenLongestIncSubs(arr,n));
s.close();
}

public static int lenLongestIncSubs(int[] arr, int n) {
int dp[] = new int[n]; // this will store the result for each array index
// i.e. length of inc subseq. till that index considering it the last element of array...
int max = 1;
for(int i=0;i<n;i++){
// initializing all elements result with 1 considering a single element is always in inc seq.
dp[i] =1;

/* here we will have to go to each element before the current one
*to check if it is smaller than current one.. and if it is
*then we will add its result to the current one..
*and we will keep on checking further and in last result at ith index
*will be updated with max len of inc subs. till itself..
*/
for(int j=0;j<i;j++){
// checking if jth element is smaller than ith so as to form inc subs.
if(arr[j]<arr[i]){
// we will only update the res when new res is greater than previous..
if(dp[i]<dp[j]+1){
dp[i] = dp[j]+1;
}
// updating our max if it becomes less than current result at ith element...
if(max<dp[i])
max = dp[i];
}
}
}
return max;
}

}
60 changes: 60 additions & 0 deletions Miscellaneous/gcd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
public class GcdEuclid(){
//GCD of two numbers a,b is the largest number d that divides both of them.
// A simple way to find GCD is to factorize both numbers and multiply common factors.
// Doing the simple way takes a lot more time generally O(min(a,b)) which is not accepted when input numbers are
// too large .
// whereas worst case time complexity of euclid algo is O(log(min(a,b)))
// Euclid algo is based on two facts:
// If we subtract smaller number from larger (we reduce larger number), GCD doesn�t change....
// .....So if we keep subtracting repeatedly the larger of two, we end up with GCD.
// Now instead of subtraction, if we divide smaller number, the algorithm stops when we find remainder 0.

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.println("Enter first no: ");
int a = s.nextInt();
System.out.println("Enter second no: ");
int b = s.nextInt();

// noting starting time before calling function.
Long start = System.currentTimeMillis();
System.out.println( "GCD using simple method: " + gcd(a,b));
// noting ending time of finding GCD with simple method
Long end = System.currentTimeMillis();
// time taken by the function call to return GCD
System.out.println("Time taken by using simple method: "+ (end-start));

// finding GCD using Euclid way
System.out.println("GCD using euclid algorithm: "+ gcdEuclid(a, b));
// time taken will be current time - last time..
System.out.println("Time taken by using simple method: "+(System.currentTimeMillis()-end));
s.close();
}

public static int gcdEuclid(int a , int b){
if(a<b)
return gcdEuclid(b, a);
if(b==0)
return a;
return gcdEuclid(b, a%b);
}

// this is the naive way of finding gcd that takes
public static int gcd(int a,int b){
if(a>b){
int temp = a;
a = b;
b = temp;
}
int gcdr=1;
for(int i=2;i<=a;i++){
while(a%i==0 && b%i==0)
{
gcdr*=i;
a/=i;
}
}
return gcdr;
}
}
31 changes: 31 additions & 0 deletions search-algos/binary_search/BinarySearch_Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

-->Binary Search is a searching algorithm that can find a required element in an array in O(log(n)) time
where n is the number of elements present in array.
-->In this we will be given with an sorted array we will search the element by dividing our region of interest
i.e. array in two halves.We will use indices to limit our search i.e start, mid and end.
-->Initially we will set start to 0 and end to n (array size).. mid will be the average of start and end.
-->We will verify if element is present at mid then we will return the index else we will shift our starting
point or ending point accordingly. Like if the array is sorted in increasing order.Then if currently element
x to be found is greater than current element at mid then it means it will only be present in later half of
array so we will shift our start after mid i.e mid+1.. else we will look for first half if element is smaller
than mid element i.e end=mid-1... we will continously repeat these steps until we find the presence of element
or if start gets bigger than end then our search gets finished.

-->We can implement binary search recursively or iteratively too i have shown both implementations
of the same in the code binarySearch.java ..

for example for this code..

/* INPUT:
enter the size of the array : 5
enter element 1 : 1
enter element 2 : 2
enter element 3 : 3
enter element 4 : 4
enter element 5 : 5
Enter element to be searched : 4

OUTPUT:
Index from iterative binarySearch : 3
Index from recursive binarySearch : 3
*/
86 changes: 86 additions & 0 deletions search-algos/binary_search/binarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import java.util.Scanner;

public class binarysearch {

public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("enter the size of the array : ");
int size= s.nextInt();
int input[]=new int[size];
// taking inputs for array
// for binary search the array must be sorted initially..
System.out.println("Enter the array in increasing order only);
for(int i=0;i<size;i++)
{ System.out.print("enter element "+ (i+1) +" : ");
input[i]= s.nextInt();
}

System.out.print("Enter element to be searched : ");
int element= s.nextInt();

//finding result using iterative method
int resIterative=binarySearchIterative(input,element);
System.out.println("Index from iterative binarySearch : "+resIterative);

//finding result using recursive method
int resRecursive=binarySearchRecursive(input,element,0,size);
System.out.println("Index from recursive binarySearch : "+resRecursive);
System.out.println("in Sorted Input array-->>");
for(int i=0;i<size;i++)
{ System.out.print(input[i]+ " ");
}
s.close();
}

public static int binarySearchIterative(int[] arr,int X) {
int index;
int start=0,end=arr.length-1;
int mid;

while(start<=end) // if start>end then it means that our search is finished and element not found
{ mid=(start+end)/2;

if(arr[mid]==X)
{ index=mid; // if element is found we will return index
return index+1;
} // array is sorted in increasing order so if
else if(X<arr[mid]){ // the element is lesser than element at mid
end=mid-1; // that means it is present in first half
}else{ // else it is in second half
start =mid+1;
}
}
return -1; // if element not found at last
}

public static int binarySearchRecursive(int arr[],int element,int start,int end){

if(start>end) // if search finished and element not found
return -1;
int mid= (start + end)/2; // defining the mid index each time when function is called.
if(arr[mid] == element){ // if element found
return mid+1;
}else if(arr[mid]<element){ // if element greater than element at mid
start = mid+1; // shift start to second half
// search in second half and return what comes..
return binarySearchRecursive(arr, element,start,end);
}else{ // if element smaller than element at mid
end = mid-1; // shift start to first half
// search in first half and return what comes..
return binarySearchRecursive(arr, element,start,end);
}
}
}
/* INPUT:
enter the size of the array : 5
enter element 1 : 1
enter element 2 : 2
enter element 3 : 3
enter element 4 : 4
enter element 5 : 5
Enter element to be searched : 4

OUTPUT:
Index from iterative binarySearch : 3
Index from recursive binarySearch : 3
*/