Delete OrderAgnosticBinary Search.java

pull/1145/head
Udaya Krishnan.M 2023-01-31 02:07:34 +05:30 committed by GitHub
parent c9d70b3eff
commit e68213c8dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 0 additions and 43 deletions

View File

@ -1,43 +0,0 @@
public class OrderAgnosticBS {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
int target=7;
int ans=orderAgnosticBS(arr,target);
System.out.println(ans);
}
static int orderAgnosticBS(int[] arr,int target){
int start=0;
int end=arr.length-1;
//find arr sorted in ascend or descend order
boolean isAsc;// boolean isAsc = arr[start]<arr[send];
if(arr[start]<arr[end]){
isAsc=true;
}
else{
isAsc=false;
}
while(start<=end){
int mid=start+(end-start)/2;
if(arr[mid]==target){
return mid;
}
if(isAsc) {
if(arr[mid]>target) {
end=mid-1;
}
else if(arr[mid]<target){
start=mid+1;
}
}
else{
if(arr[mid]<target) {
end=mid-1;
}
else if(arr[mid]>target){
start=mid+1;
}
}
}
return -1;
}
}