Skip to content

Commit 5ea83b2

Browse files
committed
added changes like file formatting and variables descriptively
1 parent dc30e7d commit 5ea83b2

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/main/java/com/thealgorithms/searches/agnosticBinarySearch.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,31 @@ public static void main(String[] args) {
1111
arr[i]=sc.nextInt();
1212
}
1313
System.out.println("enter key element to search in a array");
14-
int k=sc.nextInt();
14+
int key=sc.nextInt();
1515
sc.close();
16-
int m =agnosticbinarySearch(arr,k);
17-
System.out.println(m);
16+
int keyIndexPosition =agnosticbinarySearch(arr,key);
17+
System.out.println(keyIndexPosition);
1818

1919
}
20-
public static int agnosticbinarySearch(int arr[],int k){
20+
public static int agnosticbinarySearch(int arr[],int key){
2121
int start = 0;
2222
int end = arr.length-1;
23-
boolean ac=arr[start]>arr[end];
23+
boolean arrDescending=arr[start]>arr[end]; //checking for Array is in ascending order or descending order.
2424
while(start<=end){
2525
int mid = end-start/2;
26-
if (arr[mid]==k){
26+
if (arr[mid]==key){
2727
return mid;
2828
}
29-
if(ac){
30-
if(k<arr[mid]){
31-
start=mid+1;
29+
if(arrDescending){ // boolean is true then our array is in descending order
30+
if(key<arr[mid]){
31+
start=mid+1;
32+
}
33+
else{
34+
end=mid-1;
35+
}
3236
}
33-
else{
34-
end=mid-1;
35-
}}
36-
else {
37-
if(k>arr[mid]){
37+
else { // otherwise our array is in ascending order
38+
if(key>arr[mid]){
3839
start=mid+1;
3940
}
4041
else{

0 commit comments

Comments
 (0)