
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search ArrayList Element Using Binary Search in Java
Searching is a sorting process to find a particular information from a set of random elements. In a Linear Data structure, there are two types of searching process -
Linear Search
Binary Search
Linear search is a simple searching method by which we can find an element from a data source in a sequential manner. For this searching process the best execution time is 1 and worse is always considered as n.
Binary search is a searching process of a particular key element from a stock of multiple elements which follows divide and conquer approach. In this search process the algorithm runs in a repeat manner. The interval of the search process will be half here.
Here in this article we will learn about how to search an ArrayList by using Binary Search method in Java.
What Is Binary Search and how it works in Java?
A binary search is a technique to search a targeted element in a data collection.
Binary data search is the most acceptable and used technique. It is faster than the liner search.
A recursive binary search is a recursion technique where the entire process will run until the targeted element is found.
In general, Binary Search performed by dividing an array into some halves.
When the memory space is low, we can use this process then.
Algorithm
Step 1 ? Start.
Step 2 ? Sort an array following an ascending order.
Step 3 ? Set low index to the first element.
Step 4 ? Set high index to the last element.
Step 5 ? With low or high indication set average of the middle index.
Step 6 ? If the targeted element is in middle. Return middle.
Step 7 ? If the targeted element is less than middle element, set -1 for high to middle.
Step 8 ? If the targeted element is greater than middle element, set +1 for low to middle.
Step 9 ? Repeat until the searching conditions satisfied.
Step 10 ? Else, it is clear then element not present.
Syntax of Binary Search - Through Iteration
mid = (low + high)/2 if (x == arr[mid]) return mid else if (x > arr[mid]) low = mid + 1 else high = mid - 1
Syntax of Binary Search - Through Recursion
binarySearch(arr, z, low, high) if low > high return False else mid = (low + high) / 2 if z == arr[mid] return mid else if z > arr[mid] return binarySearch(arr, z, mid + 1, high) else return binarySearch(arr, z, low, mid - 1)
There are two different approaches to search an element from an ArrayList by using Binary Search. Above we have mentioned the syntax of those methods to get a bigger picture how the process of Binary Search really works in a Java environment.
Approach
Approach 1 ? A General Binary Search Program
Approach 2 ? Binary Search Using Iteration
Approach 3 ? Binary Search Using Recursion
A General Binary Search Program
Here in this Java build code, we have tried to make you understand how a Binary Search program really works in a Java environment. Hope you will understand the whole process and algorithm mentioned here.
Example 1
import java.io.*; import java.util.*; public class binarysearchbyrdd { static boolean search(int key, ArrayList<Integer> B){ int low = 0; int high = B.size() - 1; while (low <= high) { int mid = low + (high - low) / 2; if (B.get(mid) == key) { return true; } else if (B.get(mid) < key) { low = mid + 1; } else { high = mid - 1; } } return false; } public static void main(String[] args){ ArrayList<Integer> R = new ArrayList<>(); R.add(10); R.add(16); R.add(07); R.add(01); R.add(97); R.add(22); R.add(23); R.add(9); int key = 19; boolean check = search(key, R); System.out.println(check); int key7 = 2; boolean check7 = search(key7, R); System.out.println(check7); } }
Output
false false
Binary Search Using Iteration Method
Binary search with Iteration (Process) ?
Given value to be compared with the element to be searched.
If it is a match then we get the value.
If the value is greater than the middle element then shift to right subarray.
If less than then follow left subarray.
If there is no return, end the process.
Example 2
import java.io.*; import java.util.*; public class BinarytpSearch { int binarytpSearch(ArrayList<Integer> arr, int x) { int left = 0, right = arr.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr.get(mid) == x) return mid; if (arr.get(mid) < x) left = mid + 1; else right = mid - 1; } return -1; } public static void main(String args[]) { BinarytpSearch ob = new BinarytpSearch(); ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(16); arr.add(10); arr.add(97); arr.add(07); arr.add(10); arr.add(2001); arr.add(2022); int x = 10; System.out.println("The elements of the arraylist here are: "+arr); int result = ob.binarytpSearch(arr, x); if (result == -1) System.out.println("Element not present"); else System.out.println("The Element " + x + " is found at "+ "index " + result); } }
Output
The elements of the arraylist here are: [16, 10, 97, 7, 10, 2001, 2022] The Element 10 is found at index 4
Binary Search Using Recursion
Binary search with Recursion (Process) ?
Compare the element to be searched with the middle element.
If it matches then return the mid index.
Else, the number is greater than the mid element then right subarry will follow.
Else recur left half.
Example 3
import java.io.*; import java.util.*; public class Binary0Search { int binary0Search(ArrayList<Integer> arr, int l1, int r2, int x7){ if (r2 >= l1){ int mid = l1 + (r2 - l1) / 2; if (arr.get(mid) == x7) return mid; if (arr.get(mid) > x7) return binary0Search(arr, l1, mid - 1, x7); return binary0Search(arr, mid + 1, r2, x7); } return -1; } public static void main(String args[]){ Binary0Search ob = new Binary0Search(); ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(16); arr.add(10); arr.add(97); arr.add(07); arr.add(10); arr.add(2001); arr.add(2022); int n = arr.size(); int x7 = 10; System.out.println("The elements present in the arraylist are: "+arr); int result = ob.binary0Search(arr,0,n-1,x7); if (result == -1) System.out.println("Element not present here, Sorry!"); else System.out.println("The Element " + x7 + " is found at "+ "index number" + result); } }
Output
The elements present in the arraylist are: [16, 10, 97, 7, 10, 2001, 2022] The Element 10 is found at index number4
Conclusion
In this article we have learnt how to do the binary search in Java. And by using Iterative and Recursive binary search, we built some Java code as per the algorithm. Hope it will give you a better view to understand how to Search an ArrayList Element Using Binary Search in a Java environment.