-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.java
More file actions
50 lines (38 loc) · 883 Bytes
/
binarySearch.java
File metadata and controls
50 lines (38 loc) · 883 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package algorithms;
import java.util.Arrays;
//import java.util.Random;
public class binarySearch {
public int binarySearchMethod(int arr[], int num, int l, int r)
{
if(l<=r)
{
int mid =(l+ (r))/2;
System.out.println(mid);
if( arr[mid] > num)
{
r = mid-1;
return binarySearchMethod(arr,num,l,r);
}
else if(arr[mid] < num)
{
l = mid+1;
return binarySearchMethod(arr,num,l,r);
}
else if(arr[mid] == num)
{
return mid+1;
}
}
return -1;
}
public static void main(String args[])
{
int arr[] = {2,45,54,50,62,78,85,87,89,90,99,800,776,9090};
int num = 50;
System.out.println(Arrays.toString(arr));
System.out.println(num);
binarySearch bs = new binarySearch();
System.out.println(arr.length);
System.out.println("the binary search result is"+bs.binarySearchMethod(arr, num, 0, arr.length));
}
}