Skip to content

Commit c5312f6

Browse files
committed
Meta Binary Search
1 parent 33dee07 commit c5312f6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.searches;
2+
3+
public class MetaBinarySearch {
4+
5+
// To find the index of an item from a sorted list
6+
7+
public static int metaBinarySearch(int[] arr, int target) {
8+
int left = 0;
9+
int right = arr.length - 1;
10+
11+
while (left <= right) {
12+
int mid = left + (right - left) / 2;
13+
if (arr[mid] == target)
14+
return mid;
15+
16+
if (arr[mid] > target) {
17+
right = mid - 1;
18+
} else {
19+
left = mid + 1;
20+
}
21+
}
22+
23+
return -1;
24+
}
25+
26+
public static void main(String[] args) {
27+
int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
28+
int target = 23;
29+
30+
int index = metaBinarySearch(arr, target);
31+
32+
if (index != -1) {
33+
System.out.println("Target found at index: " + index);
34+
} else {
35+
System.out.println("Target not found");
36+
}
37+
}
38+
39+
}

0 commit comments

Comments
 (0)