Searching Algorithms in Java
Last Updated :
10 Nov, 2022
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 and every element is checked. For Example: Linear Search.
- Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the center of the search structure and divide the search space in half. For Example: Binary Search.
Linear Search: The idea is to traverse the given array arr[] and find the index at which the element is present. Below are the steps:
- Let the element to be search be x.
- Start from the leftmost element of arr[] and one by one compare x with each element of arr[].
- If x matches with an element then return that index.
- If x doesn’t match with any of elements then return -1.
Below is the implementation of the Sequential Search in Java:
Java
class GFG {
public static int search( int arr[], int x)
{
int n = arr.length;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == x)
return i;
}
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 2 , 3 , 4 , 10 , 40 };
int x = 10 ;
int result = search(arr, x);
if (result == - 1 )
System.out.print(
"Element is not present in array" );
else
System.out.print( "Element is present"
+ " at index "
+ result);
}
}
|
Output:
Element is present at index 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Binary Search: This algorithm search element in a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. Below are the steps:
- Compare x with the middle element.
- If x matches with 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 right half.
- Else (x is smaller) recur for the left half.
Recursive implementation of Binary Search
Java
class BinarySearch {
int binarySearch( int arr[], int l,
int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2 ;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l,
mid - 1 , x);
return binarySearch(arr, mid + 1 ,
r, x);
}
return - 1 ;
}
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2 , 3 , 4 , 10 , 40 };
int n = arr.length;
int x = 10 ;
int result = ob.binarySearch(arr, 0 ,
n - 1 , x);
if (result == - 1 )
System.out.println( "Element "
+ "not present" );
else
System.out.println( "Element found"
+ " at index "
+ result);
}
}
|
Output:
Element found at index 3
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Iterative implementation of Binary Search
Java
class BinarySearch {
int binarySearch( int arr[], int x)
{
int l = 0 , r = arr.length - 1 ;
while (l <= r) {
int m = l + (r - l) / 2 ;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1 ;
else
r = m - 1 ;
}
return - 1 ;
}
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2 , 3 , 4 , 10 , 40 };
int n = arr.length;
int x = 10 ;
int result = ob.binarySearch(arr, x);
if (result == - 1 )
System.out.println( "Element not present" );
else
System.out.println( "Element found at index "
+ result);
}
}
|
Output:
Element found at index 3
Time Complexity: O(log N)
Auxiliary Space: O(1)
Please Login to comment...