This repository was archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecSearchTest.java
More file actions
73 lines (60 loc) · 2.03 KB
/
RecSearchTest.java
File metadata and controls
73 lines (60 loc) · 2.03 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
68
69
70
71
72
73
// Main class
class RecSearchTest {
// Method 1
// Recursive binary search
// Returns index of x if it is present
// in arr[l..r], else return -1
int binarySearch(String[] arr, int l, int r, String x)
{
// Restrict the boundary of right index
// and the left index to prevent
// overflow of indices
if (r >= l && l <= arr.length - 1) {
int mid = l + (r - l) / 2;
int res = x.compareTo(arr[mid]);
// If the element is present
// at the middle itself
if (res == 0) {
return mid;
}
// If element is smaller than mid, then it can
// only be present in left subarray
if (res < 0 ) {
return binarySearch(arr, l, mid - 1, x);
}
// Else the element can only be present
// in right subarray
else if (res > 0) {
return binarySearch(arr, mid + 1, r, x);
}
}
// We reach here when element is not present in
// array
return -1;
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Creating object of above class
RecSearchTest ob = new RecSearchTest();
// Custom input array
String arr[] = {"abs","bear","cringe","dare","enough","feeling" };
// Length of array
int n = arr.length;
// Custom element to be checked
// whether present or not
String x = "feeling";
// Calling above method
int result = ob.binarySearch(arr, 0, n - 1, 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);
}
}