Skip to content

Commit

Permalink
Merge pull request codemistic#196 from sajjad-njr/main
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
gantavyamalviya authored Oct 2, 2022
2 parents 02ebae6 + 76b5707 commit c83c54b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Java/Searching-Algorithms/oderAgonosticBinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Arrays;

public class BinaraySearch {

public static void main(String[] args) {
int[] arr = {5,4,3,2,1};
int target = 6;

oderAgonosticBS(arr,6);
}


static int oderAgonosticBS(int [] arr , int target)
{
int start = 0;
int end = arr.length - 1;

boolean isAsc = arr[start] < arr[end];
/*
boolean isAsc ;
if(= arr[start] < arr[end] )
{
isAsc = true;
}isAsc = false;
*/

while(start <= end)
{
int mid = start + (end - start)/2;
if(target == arr[mid]) 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 start ;
}
}

0 comments on commit c83c54b

Please sign in to comment.