-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_BinarySearch_In_Java.java
More file actions
49 lines (49 loc) · 1.08 KB
/
Copy path02_BinarySearch_In_Java.java
File metadata and controls
49 lines (49 loc) · 1.08 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
class binarysearch
{
public static int bs(int arr[],int target,int low,int high)
{
int mid = (low+high)/2;
if(low>mid)
{
return -1;
}
else if(arr[mid] < target)
{
return bs(arr,target,mid+1,high);
}
else if(arr[mid] > target)
{
return bs(arr,target,low,mid-1);
}
else
{
return mid;
}
}
public static int square(int n)
{
return n*n;
}
public static void main(String args[])
{
Scanner babu = new Scanner(System.in);
System.out.print("Enter a value to find: ");
int n = babu.nextInt();
int arr[] = new int[101];
for(int i=0;i<=100;i++)
{
arr[i] = square(i);
}
int index = bs(arr,n,0,arr.length-1);
if (index != -1)
{
System.out.print(n+" found at index: "+index);
}
else
{
System.out.print(n+" is not found");
}
babu.close();
}
}