-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary Search 1
More file actions
34 lines (32 loc) · 929 Bytes
/
Binary Search 1
File metadata and controls
34 lines (32 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//given an array of n element, find a particular element using binary search
import java.util.*;
class Test2{
static int binarySearch(int key, int arr[],int l,int h){
int mid=l+(h-l)/2;
if(h>=l){
if(arr[mid]==key)
return mid;
else if (arr[mid]>key)
return binarySearch(key, arr,l,mid-1);
else
return binarySearch(key, arr,mid+1,h);
}
return -1;
}
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n=sc.nextInt();
System.out.print("Enter the elements in the array: ");
int a[]=new int [n];
for(int i=0; i<n; i++)
a[i]=sc.nextInt();
System.out.print("Enter the elements to search: ");
int key=sc.nextInt();
int result=binarySearch(key, a,0,n-1);
if(result ==-1)
System.out.print("Element NOT found");
else
System.out.print("Element found at index number "+result);
}
}