Introduction to Searching
Last Updated :
23 Sep, 2025
Searching is the fundamental process of locating a specific element or item within a collection of data. This collection of data can take various forms, such as arrays, lists, trees, or other structured representations.
Basic Searching Algorithms:
Linear Search – O(n) Time and O(1) Space
It is the simplest searching algorithm that checks each element sequentially until the key is found or the collection is fully traversed. Works on both sorted and unsorted data. For more details refer here.
For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
C++
#include <iostream>
#include <vector>
using namespace std;
int search(vector<int>& arr, int x) {
// Iterate over the array in order to
// find the key x
for (int i = 0; i < arr.size(); i++)
if (arr[i] == x)
return i;
return -1;
}
int main() {
vector<int> arr = {2, 3, 4, 10, 40};
int x = 10;
int res = search(arr, x);
if (res == -1)
cout << "Element is not present in the array";
else
cout << "Element is present at index " << res;
return 0;
}
C
#include <stdio.h>
int search(int arr[], int n, int x) {
// Iterate over the array in order to
// find the key x
for (int i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
int main(void) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, n, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Java
import java.io.*;
class GFG {
public static int search(int arr[], int N, int x)
{
// Iterate over the array in order to
// find the key x
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, arr.length, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
System.out.print("Element is present at index "
+ result);
}
}
Python
def search(arr, x):
n = len(arr)
# Iterate over the array in order to
# find the key x
for i in range(0, n):
if (arr[i] == x):
return i
return -1
if __name__ == "__main__":
arr = [2, 3, 4, 10, 40]
x = 10
result = search(arr, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)
C#
using System;
class GFG {
public static int search(int[] arr, int x) {
int n = arr.Length;
// Iterate over the array in order to
// find the key x
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public static void Main() {
int[] arr = { 2, 3, 4, 10, 40 };
int x = 10;
int result = search(arr, x);
if (result == -1)
Console.WriteLine(
"Element is not present in array");
else
Console.WriteLine("Element is present at index "
+ result);
}
}
JavaScript
function search(arr, x) {
const n = arr.length;
// Iterate over the array in order to
// find the key x
for (let i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver Code
let arr = [ 2, 3, 4, 10, 40 ];
let x = 10;
let result = search(arr, x);
(result == -1)
? console.log("Element is not present in array")
: console.log("Element is present at index " + result);
PHP
<?php
function search($arr, $x) {
$n = sizeof($arr);
// Iterate over the array in order to
// find the key x
for($i = 0; $i < $n; $i++) {
if($arr[$i] == $x)
return $i;
}
return -1;
}
$arr = array(2, 3, 4, 10, 40);
$x = 10;
$result = search($arr, $x);
if($result == -1)
echo "Element is not present in array";
else
echo "Element is present at index " ,
$result;
?>
OutputElement is present at index 3
Binary Search – O(log n) Time and O(1) Space
It works on sorted arrays by repeatedly dividing the search interval in half. If the target matches the middle element, return it; otherwise, continue searching left or right. For more details refer here.
For example: Consider the array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the key = 23.
C++
#include <iostream>
#include <vector>
using namespace std;
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
int binarySearch(vector<int> &arr, int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 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, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
return -1;
}
int main() {
vector<int> arr = { 2, 3, 4, 10, 40 };
int query = 10;
int n = arr.size();
int result = binarySearch(arr, 0, n - 1, query);
if (result == -1) cout << "Element is not present in array";
else cout << "Element is present at index " << result;
return 0;
}
C
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 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, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) printf("Element is not present in array");
else printf("Element is present at index %d", result);
return 0;
}
Java
class GFG {
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
static int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 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, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
// We reach here when element is not present
// in array
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println(
"Element is not present in array");
else
System.out.println(
"Element is present at index " + result);
}
}
Python
# A recursive binary search function. It returns
# location of x in given array arr[low..high] is present,
# otherwise -1
def binarySearch(arr, low, high, x):
# Check base case
if high >= low:
mid = low + (high - low) // 2
# If 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
elif arr[mid] > x:
return binarySearch(arr, low, mid-1, x)
# Else the element can only be present
# in right subarray
else:
return binarySearch(arr, mid + 1, high, x)
# Element is not present in the array
else:
return -1
if __name__ == '__main__':
arr = [2, 3, 4, 10, 40]
x = 10
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
C#
using System;
class GFG {
// Returns index of x if it is present in
// arr[low..high], else return -1
static int binarySearch(int[] arr, int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 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, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
// We reach here when element is not present
// in array
return -1;
}
public static void Main() {
int[] arr = { 2, 3, 4, 10, 40 };
int n = arr.Length;
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
Console.WriteLine(
"Element is not present in arrau");
else
Console.WriteLine("Element is present at index "
+ result);
}
}
JavaScript
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
function binarySearch(arr, low, high, x) {
if (high >= low) {
let mid = low + Math.floor((high - low) / 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, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
// We reach here when element is not
// present in array
return -1;
}
// Driver Code
let arr = [ 2, 3, 4, 10, 40 ];
let x = 10;
let n = arr.length
let result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
console.log("Element is not present in array");
else
console.log("Element is present at index " + result);
PHP
<?php
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
function binarySearch($arr, $low, $high, $x) {
if ($high >= $low) {
$mid = ceil($low + ($high - $low) / 2);
// If the element is present
// at the middle itself
if ($arr[$mid] == $x)
return floor($mid);
// If element is smaller than
// mid, then it can only be
// present in left subarray
if ($arr[$mid] > $x)
return binarySearch($arr, $low,
$mid - 1, $x);
// Else the element can only
// be present in right subarray
return binarySearch($arr, $mid + 1,
$high, $x);
}
// We reach here when element
// is not present in array
return -1;
}
$arr = array(2, 3, 4, 10, 40);
$n = count($arr);
$x = 10;
$result = binarySearch($arr, 0, $n - 1, $x);
if(($result == -1))
echo "Element is not present in array";
else
echo "Element is present at index ",
$result;
?>
OutputElement is present at index 3
Ternary Search – O(log₃ n) Time and O(1) Space
Ternary Search is a variant of binary search that splits the search interval into three parts instead of two. Mostly used in searching unimodal functions. For more details refer here.
For example: Consider the array arr[] = {9,7,1,2,3,6,10}.
C++
#include <iostream>
#include <vector>
using namespace std;
int findMinIndex(vector<int>& arr) {
int low = 0, high = arr.size() - 1;
int minIndex = -1;
while (low <= high) {
// divide the range into three parts
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
// if both mid1 and mid2 point to equal
// values narrow the search
if (arr[mid1] == arr[mid2]) {
// Move towards the center
low = mid1 + 1;
high = mid2 - 1;
// tentatively store mid1 as
// potential minimum
minIndex = mid1;
}
// if arr[mid1] < arr[mid2], the minimum lies in the
// left part (including mid1)
else if (arr[mid1] < arr[mid2]) {
high = mid2 - 1;
// update with better candidate
minIndex = mid1;
}
// is arr[mid1] > arr[mid2], the minimum lies in the
// right part (including mid2)
else {
low = mid1 + 1;
// update with better candidate
minIndex = mid2;
}
}
return minIndex;
}
int main() {
vector<int> arr = {9, 7, 1, 2, 3, 6, 10};
int idx = findMinIndex(arr);
cout << idx << endl;
return 0;
}
C
#include <stdio.h>
// Function to find the index of the minimum element
int findMinIndex(int arr[], int n) {
int low = 0, high = n - 1;
int minIndex = -1;
while (low <= high) {
// divide the range into three parts
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
// if both mid1 and mid2 point to equal
// values narrow the search
if (arr[mid1] == arr[mid2]) {
// Move towards the center
low = mid1 + 1;
high = mid2 - 1;
// tentatively store mid1 as
// potential minimum
minIndex = mid1;
}
// if arr[mid1] < arr[mid2], the minimum lies in the
// left part (including mid1)
else if (arr[mid1] < arr[mid2]) {
high = mid2 - 1;
// update with better candidate
minIndex = mid1;
}
// is arr[mid1] > arr[mid2], the minimum lies in the
// right part (including mid2)
else {
low = mid1 + 1;
// update with better candidate
minIndex = mid2;
}
}
return minIndex;
}
int main() {
int arr[] = {9, 7, 1, 2, 3, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int idx = findMinIndex(arr, n);
printf("%d\n", idx);
return 0;
}
Java
class GfG {
public static int findMinIndex(int[] arr) {
int low = 0, high = arr.length - 1;
int minIndex = -1;
while (low <= high) {
// divide the range into three parts
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
// if both mid1 and mid2 point to equal
// values narrow the search
if (arr[mid1] == arr[mid2]) {
// Move towards the center
low = mid1 + 1;
high = mid2 - 1;
// tentatively store mid1 as
// potential minimum
minIndex = mid1;
}
// if arr[mid1] < arr[mid2], the minimum lies in the
// left part (including mid1)
else if (arr[mid1] < arr[mid2]) {
high = mid2 - 1;
// update with better candidate
minIndex = mid1;
}
// is arr[mid1] > arr[mid2], the minimum lies in the
// right part (including mid2)
else {
low = mid1 + 1;
// update with better candidate
minIndex = mid2;
}
}
return minIndex;
}
public static void main(String[] args) {
int[] arr = {9, 7, 1, 2, 3, 6, 10};
int idx = findMinIndex(arr);
System.out.println(idx);
}
}
Python
def findMinIndex(arr):
low = 0
high = len(arr) - 1
minIndex = -1
while low <= high:
# divide the range into three parts
mid1 = low + (high - low) // 3
mid2 = high - (high - low) // 3
# if both mid1 and mid2 point to equal
# values narrow the search
if arr[mid1] == arr[mid2]:
# Move towards the center
low = mid1 + 1
high = mid2 - 1
# tentatively store mid1 as
# potential minimum
minIndex = mid1
# if arr[mid1] < arr[mid2], the minimum lies in the
# left part (including mid1)
elif arr[mid1] < arr[mid2]:
high = mid2 - 1
# update with better candidate
minIndex = mid1
# is arr[mid1] > arr[mid2], the minimum lies in the
# right part (including mid2)
else:
low = mid1 + 1
# update with better candidate
minIndex = mid2
return minIndex
def main():
arr = [9, 7, 1, 2, 3, 6, 10]
idx = findMinIndex(arr)
print(idx)
C#
using System;
class GfG {
public static int findMinIndex(int[] arr) {
int low = 0, high = arr.Length - 1;
int minIndex = -1;
while (low <= high) {
// divide the range into three parts
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
// if both arr[mid1] and arr[mid2] point to equal
// values narrow the search
if (arr[mid1] == arr[mid2]) {
// Move towards the center
low = mid1 + 1;
high = mid2 - 1;
// tentatively store mid1 as
// potential minimum
minIndex = mid1;
}
// if arr[mid1] < arr[mid2], the minimum lies in the
// left part (including mid1)
else if (arr[mid1] < arr[mid2]) {
high = mid2 - 1;
// update with better candidate
minIndex = mid1;
}
// is mid1 > mid2, the minimum lies in the
// right part (including mid2)
else {
low = mid1 + 1;
// update with better candidate
minIndex = mid2;
}
}
return minIndex;
}
public static void Main(string[] args) {
int[] arr = {9, 7, 1, 2, 3, 6, 10};
int idx = findMinIndex(arr);
Console.WriteLine(idx);
}
}
JavaScript
function findMinIndex(arr) {
let low = 0, high = arr.length - 1;
let minIndex = -1;
while (low <= high) {
// divide the range into three parts
let mid1 = low + Math.floor((high - low) / 3);
let mid2 = high - Math.floor((high - low) / 3);
// if both arr[mid1] and arr[mid2] point to equal
// values narrow the search
if (arr[mid1] === arr[mid2]) {
// Move towards the center
low = mid1 + 1;
high = mid2 - 1;
// tentatively store mid1 as
// potential minimum
minIndex = mid1;
}
// if arr[mid1] < arr[mid2], the minimum lies in the
// left part (including mid1)
else if (arr[mid1] < arr[mid2]) {
high = mid2 - 1;
// update with better candidate
minIndex = mid1;
}
// is mid1 > mid2, the minimum lies in the
// right part (including mid2)
else {
low = mid1 + 1;
// update with better candidate
minIndex = mid2;
}
}
return minIndex;
}
// Driver Code
const arr = [9, 7, 1, 2, 3, 6, 10];
const idx = findMinIndex(arr);
console.log(idx);
Jump Search – O(√n) Time and O(1) Space
Jump Search is another searching algorithm that can be used on sorted collections (arrays or lists). The idea is to reduce the number of comparisons by jumping ahead by fixed steps or skipping some elements in place of searching all elements. For more details refer here.
Interpolation Search - O(n) Time and O(1) Space
Interpolation Search is an efficient searching algorithm for sorted collections of data, such as arrays or lists. It is an improvement over Binary Search, particularly when the data is uniformly distributed. For more details refer here.
Fibonacci Search - O(log n) Time and O(1) Space
Fibonacci Search is an efficient searching algorithm used for finding a target value in a sorted collection, such as an array or list. It is similar in principle to Binary Search but uses Fibonacci numbers to determine the positions to be compared. For more details refer here.
Exponential Search – O(log n) Time and O(1) Space
Exponential Search is a searching algorithm designed to find a target value in a sorted collection, such as an array or list. It combines elements of Binary Search and Linear Search to efficiently locate the target, especially when its position is near the beginning of the collection. For more details refer here.
Comparison of Complexity Analysis of Searching Algorithms:
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem