Repository: TheAlgorithms/Java
Pull request #3791 (2022-12-07 08:33:53)

Added Order Agnostic Binary Search problem in Searches folder.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


ThisIsNishanth: Hey @debasishbsws, I am sorry for all these commits. I am new to JUnit and this repo, so I had to understand the errors and pass all checks. I committed all those changes in order to pass the "Build With Maven" checks. I have successfully passed all checks now. I have added a main folder and a test folder. Main folder has the non-redundant code and the test folder has the JUnit tests. Thank you for your suggestions!

Patch:
diff --git a/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java b/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java
new file mode 100644
index 00000000..39f26a97
--- /dev/null
+++ b/src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java
@@ -0,0 +1,47 @@
+package com.thealgorithms.searches;
+
+//URL: https://www.geeksforgeeks.org/order-agnostic-binary-search/
+
+/* Order Agnostic Binary Search is an algorithm where we do not know whether the given
+   sorted array is ascending or descending order.
+   We declare a boolean variable to find whether the array is ascending order.
+   In the while loop, we use the two pointer method (start and end) to get the middle element.
+   if the middle element is equal to our target element, then that is the answer.
+   If not, then we check if the array is ascending or descending order.
+   Depending upon the condition, respective statements will be executed and we will get our answer.
+ */
+
+ public class OrderAgnosticBinarySearch {
+
+     static int BinSearchAlgo(int arr[], int start, int end, int target) {
+
+         // Checking whether the given array is ascending order
+         boolean AscOrd = arr[start] < arr[end];
+
+         while (start <= end) {
+             int middle = start + (end - start) / 2;
+
+             // Check if the desired element is present at the middle position
+             if (arr[middle] == target)
+                 return middle;       // returns the index of the middle element
+
+             // Ascending order
+             if (AscOrd) {
+                 if (arr[middle] < target)
+                     start = middle + 1;
+                 else
+                     end = middle - 1;
+             }
+
+             // Descending order
+             else {
+                 if (arr[middle] > target)
+                     start = middle + 1;
+                 else
+                     end = middle - 1;
+             }
+         }
+         // Element is not present
+         return -1;
+     }
+ }


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java
new file mode 100644
index 00000000..8194d345
--- /dev/null
+++ b/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java
@@ -0,0 +1,72 @@
+package com.thealgorithms.searches;
+
+import com.thealgorithms.searches.OrderAgnosticBinarySearch;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class OrderAgnosticBinarySearchTest {
+     @Test
+     //valid Test Case
+     public void ElementInMiddle() {
+         int[] arr = {10, 20, 30, 40, 50};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 30);
+         System.out.println(answer);
+         int expected = 2;
+         assertEquals(expected, answer);
+     }
+
+     @Test
+     //valid Test Case
+     public void RightHalfDescOrder() {
+         int[] arr = {50, 40, 30, 20, 10};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
+         System.out.println(answer);
+         int expected = 4;
+         assertEquals(expected, answer);
+     }
+
+     @Test
+     //valid test case
+     public void LeftHalfDescOrder() {
+         int[] arr = {50, 40, 30, 20, 10};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
+         System.out.println(answer);
+         int expected = 0;
+         assertEquals(expected, answer);
+     }
+
+     @Test
+     //valid test case
+     public void RightHalfAscOrder() {
+         int[] arr = {10, 20, 30, 40, 50};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
+         System.out.println(answer);
+         int expected = 4;
+         assertEquals(expected, answer);
+     }
+
+     @Test
+     //valid test case
+     public void LeftHalfAscOrder() {
+         int[] arr = {10, 20, 30, 40, 50};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
+         System.out.println(answer);
+         int expected = 0;
+         assertEquals(expected, answer);
+     }
+
+     @Test
+     //valid test case
+     public void ElementNotFound() {
+         int[] arr = {10, 20, 30, 40, 50};
+         int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 100);
+         System.out.println(answer);
+         int expected = -1;
+         assertEquals(expected, answer);
+     }
+ }