Added order agnostic binary search in java file
parent
af47764be0
commit
4c76783224
|
@ -0,0 +1,47 @@
|
|||
//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.
|
Loading…
Reference in New Issue