Find a String in given Array of Strings using Binary Search
Last Updated :
19 Oct, 2023
Given a sorted array of Strings arr and a String x, find an index of x if it is present in the array, using Binary Search
Examples:
Input: arr[] = {“contribute”, “geeks”, “ide”, “practice”}, x = “ide”
Output: 2
Explanation: The String x is present at index 2.
Input : arr[] = {“contribute”, “geeks”, “ide”, “practice”}, x = “zz”
Output : -1
Explanation: The String “zz” is not present.
Approach to Find a String in a given Array of Strings:
The idea is to apply Binary Search on the given Array of Strings.
Illustrations:
Suppose the array of string is arr[] = {“contribute”, “geeks”, “ide”, “practice”}, and the key string to find is x = “ide”.
Now the above approach will be performed like this:
1st iteration:
- Low = 0, High = 4, Hence Mid = 2
- Element at index 2 = “ide”, which is equal to the key string x
- Therefore, x is found.
Below are the steps on how to Find a String in a given Array of Strings using Binary Search:
- Find the Middle element of the Array of the String, and compare it with the String x.
- If the key is found, return the index of the middle.
- Else check if the mid string is smaller or larger than the key string x.
- If it is smaller, reduce the search space to the lower half
- If it is larger, reduce the search space to greater than half
- Repeat the above steps till the key string x is found, or no more search space is left to search.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch(string arr[], string x, int n)
{
int l = 0;
int r = n - 1;
while (l <= r) {
int m = l + (r - l) / 2;
int res = -1000;
if (x == (arr[m]))
res = 0;
if (res == 0)
return m;
if (x > (arr[m]))
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main()
{
string arr[]
= { "contribute" , "geeks" , "ide" , "practice" };
string x = "ide" ;
int n = 4;
int result = binarySearch(arr, x, n);
if (result == -1)
cout << ( "Element not present" );
else
cout << ( "Element found at index " ) << result;
}
|
Java
class GFG {
static int binarySearch(String[] arr, String x)
{
int l = 0 , r = arr.length - 1 ;
while (l <= r) {
int m = l + (r - l) / 2 ;
int res = x.compareTo(arr[m]);
if (res == 0 )
return m;
if (res > 0 )
l = m + 1 ;
else
r = m - 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
String[] arr
= { "contribute" , "geeks" , "ide" , "practice" };
String x = "ide" ;
int result = binarySearch(arr, x);
if (result == - 1 )
System.out.println( "Element not present" );
else
System.out.println( "Element found at "
+ "index " + result);
}
}
|
Python3
def binarySearch(arr, x):
l = 0
r = len (arr)
while (l < = r):
m = l + ((r - l) / / 2 )
res = (x = = arr[m])
if (res = = 0 ):
return m - 1
if (res > 0 ):
l = m + 1
else :
r = m - 1
return - 1
if __name__ = = "__main__" :
arr = [ "contribute" , "geeks" ,
"ide" , "practice" ]
x = "ide"
result = binarySearch(arr, x)
if (result = = - 1 ):
print ( "Element not present" )
else :
print ( "Element found at index" ,
result)
|
C#
using System;
class GFG {
static int binarySearch(String[] arr, String x)
{
int l = 0, r = arr.Length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
int res = x.CompareTo(arr[m]);
if (res == 0)
return m;
if (res > 0)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void Main(String[] args)
{
String[] arr
= { "contribute" , "geeks" , "ide" , "practice" };
String x = "ide" ;
int result = binarySearch(arr, x);
if (result == -1)
Console.WriteLine( "Element not present" );
else
Console.WriteLine( "Element found at "
+ "index " + result);
}
}
|
PHP
<?php
function binarySearch( $arr , $x )
{
$l = 0;
$r = count ( $arr );
while ( $l <= $r )
{
$m = $l + (int)(( $r - $l ) / 2);
$res = strcmp ( $x , $arr [ $m ]);
if ( $res == 0)
return $m ;
if ( $res > 0)
$l = $m + 1;
else
$r = $m - 1;
}
return -1;
}
$arr = array ( "contribute" , "geeks" ,
"ide" , "practice" );
$x = "ide" ;
$result = binarySearch( $arr , $x );
if ( $result == -1)
print ( "Element not present" );
else
print ( "Element found at index " .
$result );
?>
|
Javascript
function binarySearch(arr, x) {
let l = 0,
r = arr.length - 1;
while (l <= r) {
let m = l + Math.floor((r - l) / 2);
let res = x.localeCompare(arr[m]);
if (res == 0)
return m;
if (res > 0)
l = m + 1;
else
r = m - 1;
}
return -1;
}
let arr = [ "contribute" , "geeks" , "ide" , "practice" ];
let x = "ide" ;
let result = binarySearch(arr, x);
if (result == -1)
console.log( "Element not present" );
else
console.log( "Element found at " +
"index " + result);
|
Output
Element found at index 2
Time Complexity: O(log(n) * len), where n = no. of string in arr & len = max length of the string for comparing two strings we need O(len) time
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...