mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Add orderAgnosticBinarySearch (#3882)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.searches;
|
||||
import java.util.*;
|
||||
public class sortOrderAgnosticBinarySearch {
|
||||
public static int find(int arr[],int key){
|
||||
int start = 0;
|
||||
int end = arr.length-1;
|
||||
boolean arrDescending = arr[start]>arr[end]; //checking for Array is in ascending order or descending order.
|
||||
while(start<=end){
|
||||
int mid = end-start/2;
|
||||
if (arr[mid]==key){
|
||||
return mid;
|
||||
}
|
||||
if(arrDescending){ // boolean is true then our array is in descending order
|
||||
if(key<arr[mid]){
|
||||
start=mid+1;
|
||||
}
|
||||
else{
|
||||
end=mid-1;
|
||||
}
|
||||
}
|
||||
else { // otherwise our array is in ascending order
|
||||
if(key>arr[mid]){
|
||||
start=mid+1;
|
||||
}
|
||||
else{
|
||||
end=mid-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user