47 lines
1.3 KiB
Java
47 lines
1.3 KiB
Java
//Order Agnostic Binary Search implementation in JAVA
|
|
|
|
public class OrderAgnosticBinarySearch {
|
|
|
|
static int orderAgnosticBinarySearch(int[] arr, int target) {
|
|
int start = 0;
|
|
int end = arr.length - 1;
|
|
|
|
// find whether the array is sorted in ascending or descending
|
|
boolean isAsc = arr[start] < arr[end];
|
|
|
|
while(start <= end) {
|
|
// find the middle element
|
|
// int mid = (start + end) / 2;
|
|
// might be possible that (start + end) exceeds the range of int in java
|
|
int mid = start + (end - start) / 2;
|
|
|
|
if (arr[mid] == target) {
|
|
return mid;
|
|
}
|
|
|
|
if (isAsc) {
|
|
if (target < arr[mid]) {
|
|
end = mid - 1;
|
|
} else {
|
|
start = mid + 1;
|
|
}
|
|
} else {
|
|
if (target > arr[mid]) {
|
|
end = mid - 1;
|
|
} else {
|
|
start = mid + 1;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int[] arr = {1, 5, 10, 20, 30};
|
|
int target = 30;
|
|
int ans = orderAgnosticBinarySearch(arr, target);
|
|
System.out.println(ans);
|
|
}
|
|
}
|
|
|
|
//rename to OrderAgnosticBinarySearch to run.
|