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
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
*/
89 changes: 89 additions & 0 deletions search-algos/binary_search/binarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import java.util.Scanner;
import java.util.Arrays;

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(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();

//for binary search the array must be sorted ..
// using the inbuilt java library for sorting the array fast.
Arrays.sort(input);

//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
*/