-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbinarysearchusingrecursion.java
More file actions
67 lines (52 loc) · 1.5 KB
/
binarysearchusingrecursion.java
File metadata and controls
67 lines (52 loc) · 1.5 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class binarysearchusingrecursion {
// Method 1
// Returns index of x
// if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
// Checking element in whole array
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller,
// element is on left side
// so ignore right half
else
r = m - 1;
}
// If we reach here,
// element is not present
return -1;
}
// Method 2
// Main driver method
public static void main(String args[])
{
GFG ob = new GFG();
// Input array
int arr[] = { 2, 3, 4, 10, 40 };
// Length of array
int n = arr.length;
// Element to be checked if present or not
int x = 10;
// Calling the method 1 and
// storing result
int result = ob.binarySearch(arr, x);
// Element present
if (result == -1)
// Print statement
System.out.println("Element not present");
// Element not present
else
// Print statement
System.out.println("Element found at index "
+ result);
}
}