File tree 1 file changed +15
-14
lines changed
src/main/java/com/thealgorithms/searches 1 file changed +15
-14
lines changed Original file line number Diff line number Diff line change @@ -11,30 +11,31 @@ public static void main(String[] args) {
11
11
arr [i ]=sc .nextInt ();
12
12
}
13
13
System .out .println ("enter key element to search in a array" );
14
- int k =sc .nextInt ();
14
+ int key =sc .nextInt ();
15
15
sc .close ();
16
- int m =agnosticbinarySearch (arr ,k );
17
- System .out .println (m );
16
+ int keyIndexPosition =agnosticbinarySearch (arr ,key );
17
+ System .out .println (keyIndexPosition );
18
18
19
19
}
20
- public static int agnosticbinarySearch (int arr [],int k ){
20
+ public static int agnosticbinarySearch (int arr [],int key ){
21
21
int start = 0 ;
22
22
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.
24
24
while (start <=end ){
25
25
int mid = end -start /2 ;
26
- if (arr [mid ]==k ){
26
+ if (arr [mid ]==key ){
27
27
return mid ;
28
28
}
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
+ }
32
36
}
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 ]){
38
39
start =mid +1 ;
39
40
}
40
41
else {
You can’t perform that action at this time.
0 commit comments