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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Like