-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchOrderAgnostic.java
More file actions
48 lines (37 loc) · 968 Bytes
/
BinarySearchOrderAgnostic.java
File metadata and controls
48 lines (37 loc) · 968 Bytes
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
package com.vinay.practice.lc;
public class BinarySearchOrderAgnostic {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] arr = {1,2,3,4,5,6,7,8,9};
int[] arr = {9,8,7,6,5,4,3,2,1};
int target = 8;
System.out.println("The target number was found at: " + orderAgnosticBS(arr, target));
}
// don't know if order is ascending or descending
public static int orderAgnosticBS(int[] arr, int target) {
int start = 0;
int end = arr.length-1;
boolean isAsc = arr[start] < arr[end];
while(start <= end) {
// System.out.println("s");
int mid = start + (end-start) / 2;
if(arr[mid] == target) {
return mid;
}
if(isAsc) {
if(target < arr[mid]) {
end = mid-1;
} else if(target > arr[mid]) {
start = mid+1;
}
} else {
if(target > arr[mid]) {
end = mid-1;
} else if(target < arr[mid]) {
start = mid+1;
}
}
}
return -1;
}
}