Skip to content

Commit d3b112f

Browse files
authored
Create MetaBinarySearch.java
1 parent 5246f63 commit d3b112f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.searches;
2+
3+
public class MetaBinarySearch {
4+
5+
// Implementing Meta Binary Search algorithm
6+
public int metaBinarySearch(int[] sortedArray, int target) {
7+
if (sortedArray == null || sortedArray.length == 0) {
8+
return -1; // Array is empty or null
9+
}
10+
11+
int left = 0;
12+
int right = sortedArray.length - 1;
13+
14+
while (left <= right) {
15+
int mid = left + (right - left) / 2;
16+
17+
if (sortedArray[mid] == target) {
18+
return mid; // Target found
19+
} else if (sortedArray[mid] < target) {
20+
left = mid + 1; // Move to the right half
21+
} else {
22+
right = mid - 1; // Move to the left half
23+
}
24+
}
25+
26+
return -1; // Target not found
27+
}
28+
}

0 commit comments

Comments
 (0)