Java Program for Binary Search (Recursive and Iterative) Last Updated : 13 Jun, 2022 Comments Improve Suggest changes Like Article Like Report So as we all know binary search is one of the searching algorithms that is most frequently applied while dealing with data structures where the eccentric goal is not to traverse the whole array. Here array must be sorted as we check the middle element and ignore the half of the array which is of no use as per the number system. We basically ignore half of the elements just after one comparison. So do we keep on iterating till the element is found or land upon a conclusion that element is not present n the array. Algorithms: Compare x with the middle element.If x matches with the middle element, we return the mid index.Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half.Else (x is smaller) recur for the left half.Example 1 Java // Java Program to Illustrate // Iterative Binary Search // Main class // BinarySearch class GFG { // Method 1 // Returns index of x // if it is present in arr[], // else return -1 int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; // Checking element in whole array while (l <= r) { int m = l + (r - l) / 2; // Check if x is present at mid if (arr[m] == x) return m; // If x greater, ignore left half if (arr[m] < x) l = m + 1; // If x is smaller, // element is on left side // so ignore right half else r = m - 1; } // If we reach here, // element is not present return -1; } // Method 2 // Main driver method public static void main(String args[]) { GFG ob = new GFG(); // Input array int arr[] = { 2, 3, 4, 10, 40 }; // Length of array int n = arr.length; // Element to be checked if present or not int x = 10; // Calling the method 1 and // storing result int result = ob.binarySearch(arr, x); // Element present if (result == -1) // Print statement System.out.println("Element not present"); // Element not present else // Print statement System.out.println("Element found at index " + result); } } OutputElement found at index 3 Time Complexity: O(log n) Auxiliary Space: O(1) Example 2 Java // Java Program to Illustrate Recursive Binary Search // Importing required classes import java.util.*; // Main class class GFG { // Method 1 // Recursive binary search // Returns index of x if it is present // in arr[l..r], else return -1 int binarySearch(int arr[], int l, int r, int x) { // Restrict the boundary of right index // and the left index to prevent // overflow of indices if (r >= l && l <= arr.length - 1) { int mid = l + (r - l) / 2; // If the element is present // at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can // only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not present in // array return -1; } // Method 2 // Main driver method public static void main(String args[]) { // Creating object of above class GFG ob = new GFG(); // Custom input array int arr[] = { 2, 3, 4, 10, 40 }; // Length of array int n = arr.length; // Custom element to be checked // whether present or not int x = 10; // Calling above method int result = ob.binarySearch(arr, 0, n - 1, x); // Element present if (result == -1) // Print statement System.out.println("Element not present"); // Element not present else // Print statement System.out.println("Element found at index " + result); } } OutputElement found at index 3 Time Complexity: O(log n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program for Binary Search (Recursive and Iterative) kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Program to Construct a Binary Search Tree Binary Search Tree (BST) is the widely used data structure in computer science, primarily known for the efficient search, insertion, and deletion operations. It is the type of binary tree where each node has at most two children, referred to as the left child and the right child. Binary Search Tree 6 min read Java Program to Perform the Inorder Tree Traversal The binary tree is the hierarchical data structure in which each node has at most two children and it can referred to as the left child and the right child. Inorder tree traversal is one of the fundamental ways to visit all the nodes in the binary tree. It can specifically visiting the nodes in the 4 min read Implementing a Binary Tree in Java A binary tree is a hierarchical data structure composed of the nodes. Each node contains the value and references to its left child node and right child node, which are also binary trees that are possibly null. The structure resembles the tree with the nodes branching out from a central root, where 5 min read Java Program to Perform the Postorder Tree Traversal The Binary Tree can consist of nodes where each node contains the value and the references to its left and right children. The structure is organized hierarchically with a single root node at the top and each node having at Most Two Children. In this article, we will learn to perform Postorder Tree 3 min read Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray) Arrays.binarySearch()| Set 1 Covers how to find an element in a sorted array in Java. This set will cover "How to Search a key in an array within a given range including only start index". Syntax : public static int binarySearch(data_type[] arr, int fromIndex, int toIndex, data_type key) Parameters 5 min read Arrays.binarySearch() in Java with Examples | Set 1 In Java, the Arrays.binarySearch() method searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted by the Arrays.sort() method before making this call. If it is not sorted, the results are undefined. Example:Below is a si 3 min read Searching Algorithms in Java Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories: Sequential Search: In this, the list or array is traversed sequentially a 5 min read Recursion in Java In Java, Recursion is a process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. A few Java recursion examples are Towers of Hanoi (TOH) 6 min read Collections.binarySearch() in Java with Examples java.util.Collections.binarySearch() method is a java.util.Collections class method that returns the position of an object in a sorted list.// Returns index of key in a sorted list sorted in// ascending orderpublic static int binarySearch(List slist, T key)// Returns index of key in a sorted list so 4 min read Sentinel Linear Search in Java The Linear search is a simple searching algorithm that checks every element in the list or array sequentially until the target element is found or the end of the list is reached. While linear search is not the most efficient search algorithm it is straightforward and works well for small datasets or 3 min read Like