-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
OrderAgnosticBinarySearch.java
60 lines (49 loc) · 1.81 KB
/
OrderAgnosticBinarySearch.java
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
49
50
51
52
53
54
55
56
57
58
59
60
/* This is Java program to search the element in an array using Binary Search.
Binary Search only works for sorted array (either assending or in decending order).
Where normal Binary search only works for either assending or decending array, OrderAgnostic Binary
search wroks irrespective of the order of the array, i.e will works both for assending and decending array.*/
import java.util.Scanner;
public class OrderAgnosticBS {
public static void main(String[] args) // main function. Program will start from this function.
{
int arr[] = { 123, 98, 45, 13, 05, 0, -9, -14, -25 };
Scanner sc = new Scanner(System.in);
System.out.print("Enter the target element : ");
int target = sc.nextInt(); // taking input from user
int ans = search(arr, target);
System.out.println("index" + ans);
}
static int search(int arr[], int target) // this function will implement the binary search and find the element.
{
boolean isAse = arr[0] < arr[arr.length - 1];
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (target == arr[mid]) {
return mid;
}
if (isAse) {
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;
}
}
// Output
// Enter the target element : 10
// -1 (not found)
// Enter the target element : 98
// 1 (found at index 1)
// Contributed by Abhishek Bhatt : @AbhishekBhatt072003