Skip to content

Commit 8ba295b

Browse files
Added Order Agnostic Binary Search problem in Searches folder. (TheAlgorithms#3791)
* Added Order Agnostic Binary Search problem * Update OrderAgnosticBinSearch.java * Added JUnit Tests and removed redundant code. * Made minor changes in JUnit Tests * Removed tests for main folder and added docs. * Added OrderAgnosticBinarySearchTest.java * Renamed file to avoid errors. * Updated the file to avoid build error
1 parent 219ec7c commit 8ba295b

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.searches;
2+
3+
//URL: https://www.geeksforgeeks.org/order-agnostic-binary-search/
4+
5+
/* Order Agnostic Binary Search is an algorithm where we do not know whether the given
6+
sorted array is ascending or descending order.
7+
We declare a boolean variable to find whether the array is ascending order.
8+
In the while loop, we use the two pointer method (start and end) to get the middle element.
9+
if the middle element is equal to our target element, then that is the answer.
10+
If not, then we check if the array is ascending or descending order.
11+
Depending upon the condition, respective statements will be executed and we will get our answer.
12+
*/
13+
14+
public class OrderAgnosticBinarySearch {
15+
16+
static int BinSearchAlgo(int arr[], int start, int end, int target) {
17+
18+
// Checking whether the given array is ascending order
19+
boolean AscOrd = arr[start] < arr[end];
20+
21+
while (start <= end) {
22+
int middle = start + (end - start) / 2;
23+
24+
// Check if the desired element is present at the middle position
25+
if (arr[middle] == target)
26+
return middle; // returns the index of the middle element
27+
28+
// Ascending order
29+
if (AscOrd) {
30+
if (arr[middle] < target)
31+
start = middle + 1;
32+
else
33+
end = middle - 1;
34+
}
35+
36+
// Descending order
37+
else {
38+
if (arr[middle] > target)
39+
start = middle + 1;
40+
else
41+
end = middle - 1;
42+
}
43+
}
44+
// Element is not present
45+
return -1;
46+
}
47+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.searches;
2+
3+
import com.thealgorithms.searches.OrderAgnosticBinarySearch;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.*;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
12+
public class OrderAgnosticBinarySearchTest {
13+
@Test
14+
//valid Test Case
15+
public void ElementInMiddle() {
16+
int[] arr = {10, 20, 30, 40, 50};
17+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 30);
18+
System.out.println(answer);
19+
int expected = 2;
20+
assertEquals(expected, answer);
21+
}
22+
23+
@Test
24+
//valid Test Case
25+
public void RightHalfDescOrder() {
26+
int[] arr = {50, 40, 30, 20, 10};
27+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
28+
System.out.println(answer);
29+
int expected = 4;
30+
assertEquals(expected, answer);
31+
}
32+
33+
@Test
34+
//valid test case
35+
public void LeftHalfDescOrder() {
36+
int[] arr = {50, 40, 30, 20, 10};
37+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
38+
System.out.println(answer);
39+
int expected = 0;
40+
assertEquals(expected, answer);
41+
}
42+
43+
@Test
44+
//valid test case
45+
public void RightHalfAscOrder() {
46+
int[] arr = {10, 20, 30, 40, 50};
47+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
48+
System.out.println(answer);
49+
int expected = 4;
50+
assertEquals(expected, answer);
51+
}
52+
53+
@Test
54+
//valid test case
55+
public void LeftHalfAscOrder() {
56+
int[] arr = {10, 20, 30, 40, 50};
57+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
58+
System.out.println(answer);
59+
int expected = 0;
60+
assertEquals(expected, answer);
61+
}
62+
63+
@Test
64+
//valid test case
65+
public void ElementNotFound() {
66+
int[] arr = {10, 20, 30, 40, 50};
67+
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 100);
68+
System.out.println(answer);
69+
int expected = -1;
70+
assertEquals(expected, answer);
71+
}
72+
}

0 commit comments

Comments
 (0)