Searching Elements in an Array | Array Operations
Last Updated :
22 Jan, 2024
In this post, we will look into search operation in an Array, i.e., how to search an element in an Array, such as:
- Searching in an Unsorted Array using Linear Search
- Searching in a Sorted Array using Linear Search
- Searching in a Sorted Array using Binary Search
- Searching in an Sorted Array using Fibonacci Search
Searching operations in an Unsorted Array using Linear Search
In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element, i.e. Linear Search
Coding implementation of the search operation:
C++
#include <bits/stdc++.h>
using namespace std;
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
cout << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
|
C
#include <stdio.h>
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
printf ("Element not found");
else
printf ("Element Found at Position: %d",
position + 1);
return 0;
}
|
Java
class Main {
static int findElement( int arr[], int n, int key)
{
for ( int i = 0 ; i < n; i++)
if (arr[i] == key)
return i;
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 12 , 34 , 10 , 6 , 40 };
int n = arr.length;
int key = 40 ;
int position = findElement(arr, n, key);
if (position == - 1 )
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1 ));
}
}
|
Python3
def findElement(arr, n, key):
for i in range (n):
if (arr[i] = = key):
return i
return - 1
if __name__ = = '__main__' :
arr = [ 12 , 34 , 10 , 6 , 40 ]
key = 40
n = len (arr)
index = findElement(arr, n, key)
if index ! = - 1 :
print ("Element Found at position: " + str (index + 1 ))
else :
print ("Element not found")
|
C#
using System;
class main {
static int findElement( int [] arr, int n, int key)
{
for ( int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
public static void Main()
{
int [] arr = { 12, 34, 10, 6, 40 };
int n = arr.Length;
int key = 40;
int position = findElement(arr, n, key);
if (position == -1)
Console.WriteLine("Element not found");
else
Console.WriteLine("Element Found at Position: "
+ (position + 1));
}
}
|
Javascript
function findElement( arr, n, key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
let arr = [12, 34, 10, 6, 40];
let n = arr.length;
let key = 40;
let position = findElement(arr, n, key);
if (position == - 1)
document.write("Element not found");
else
document.write("Element Found at Position: "
+ (position + 1));
|
PHP
<?php
function findElement( $arr , $n , $key )
{
$i ;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $key )
return $i ;
return -1;
}
$arr = array (12, 34, 10, 6, 40);
$n = sizeof( $arr );
$key = 40;
$position = findElement( $arr , $n , $key );
if ( $position == - 1)
echo ("Element not found");
else
echo ("Element Found at Position: " . ( $position + 1));
?>
|
Output
Element Found at Position: 5
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Searching in a Sorted Array using Linear Search
In a sorted array, the most trivial method for search operation is by using Linear Search.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 10;
int position = findElement(arr, n, key);
if (position == -1)
cout << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
|
C
#include <stdio.h>
int findElement( int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main()
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 10;
int position = findElement(arr, n, key);
if (position == -1)
printf ("Element not found");
else
printf ("Element Found at Position: %d",
position + 1);
return 0;
}
|
Java
class Main {
static int findElement( int arr[], int n, int key)
{
for ( int i = 0 ; i < n; i++)
if (arr[i] == key)
return i;
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 5 , 6 , 7 , 8 , 9 , 10 };
int n = arr.length;
int key = 10 ;
int position = findElement(arr, n, key);
if (position == - 1 )
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1 ));
}
}
|
Python3
def findElement(arr, n, key):
for i in range (n):
if (arr[i] = = key):
return i
return - 1
if __name__ = = '__main__' :
arr = [ 5 , 6 , 7 , 8 , 9 , 10 ]
key = 10
n = len (arr)
index = findElement(arr, n, key)
if index ! = - 1 :
print ("Element Found at position: " + str (index + 1 ))
else :
print ("Element not found")
|
C#
using System;
class main {
static int findElement( int [] arr, int n, int key)
{
for ( int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
public static void Main()
{
int [] arr = { 5, 6, 7, 8, 9, 10 };
int n = arr.Length;
int key = 10;
int position = findElement(arr, n, key);
if (position == -1)
Console.WriteLine("Element not found");
else
Console.WriteLine("Element Found at Position: "
+ (position + 1));
}
}
|
Javascript
function findElement( arr, n, key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
let arr = [5, 6, 7, 8, 9, 10];
let n = arr.length;
let key = 10;
let position = findElement(arr, n, key);
if (position == - 1)
document.write("Element not found");
else
document.write("Element Found at Position: "
+ (position + 1));
|
PHP
<?php
function findElement( $arr , $n , $key )
{
$i ;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $key )
return $i ;
return -1;
}
$arr = array (5, 6, 7, 8, 9, 10);
$n = sizeof( $arr );
$key = 10;
$position = findElement( $arr , $n , $key );
if ( $position == - 1)
echo ("Element not found");
else
echo ("Element Found at Position: " . ( $position + 1));
?>
|
Output
Element Found at Position: 6
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Searching in a Sorted Array using Binary Search
In a sorted array, the search operation can be performed efficiently by using binary search.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
int main()
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof (arr) / sizeof (arr[0]);
key = 10;
cout << "Index: " << binarySearch(arr, 0, n - 1, key)
<< endl;
return 0;
}
|
C
#include <stdio.h>
int binarySearch( int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
int main()
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof (arr) / sizeof (arr[0]);
key = 10;
printf ("Index: %d\n", binarySearch(arr, 0, n - 1, key));
return 0;
}
|
Java
class Main {
static int binarySearch( int arr[], int low, int high,
int key)
{
if (high < low)
return - 1 ;
int mid = (low + high) / 2 ;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1 ), high, key);
return binarySearch(arr, low, (mid - 1 ), key);
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 7 , 8 , 9 , 10 };
int n, key;
n = arr.length - 1 ;
key = 10 ;
System.out.println("Index: "
+ binarySearch(arr, 0 , n, key));
}
}
|
Python3
def binarySearch(arr, low, high, key):
mid = (low + high) / 2
if (key = = arr[ int (mid)]):
return mid
if (key > arr[ int (mid)]):
return binarySearch(arr,
(mid + 1 ), high, key)
if (key < arr[ int (mid)]):
return binarySearch(arr, low, (mid - 1 ), key)
return 0
if __name__ = = "__main__":
arr = [ 5 , 6 , 7 , 8 , 9 , 10 ]
n = len (arr)
key = 10
print ("Index:", int (binarySearch(arr, 0 , n - 1 , key)))
|
C#
using System;
public class GFG {
public static int binarySearch( int [] arr, int low,
int high, int key)
{
if (high < low) {
return -1;
}
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
}
if (key > arr[mid]) {
return binarySearch(arr, (mid + 1), high, key);
}
return binarySearch(arr, low, (mid - 1), key);
}
public static void Main( string [] args)
{
int [] arr = new int [] { 5, 6, 7, 8, 9, 10 };
int n, key;
n = arr.Length;
key = 10;
Console.WriteLine(
"Index: " + binarySearch(arr, 0, n - 1, key));
}
}
|
Javascript
<script>
function binarySearch( arr, low, high, key)
{
if (high < low)
return -1;
let mid = Math.trunc((low + high) / 2);
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
let arr = [ 5, 6, 7, 8, 9, 10 ];
let n, key;
n = arr.length;
key = 10;
document.write( "Index: " + binarySearch(arr, 0, n - 1, key)
+ "</br>");
</script>
|
PHP
<?php
function binarySearch( $arr , $low ,
$high , $key )
{
if ( $high < $low )
return -1;
$mid = (int)( $low + $high ) / 2;
if ( $key == $arr [(int) $mid ])
return $mid ;
if ( $key > $arr [(int) $mid ])
return binarySearch( $arr , ( $mid + 1),
$high , $key );
return (binarySearch( $arr , $low ,
( $mid -1), $key ));
}
$arr = array (5, 6, 7, 8, 9, 10);
$n = count ( $arr );
$key = 10;
echo "Index: ", (int)binarySearch( $arr , 0,
$n -1, $key );
?>
|
Time Complexity: O(log(n)) Using Binary Search
Auxiliary Space: O(log(n)) due to recursive calls, otherwise iterative version uses Auxiliary Space of O(1).
Searching in a Sorted Array using Fibonacci Search
Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using std::cout;
int minimum( int x, int y) { return (x < y) ? x : y; }
int fibonacciSearch( int arr[], int x, int n)
{
int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = -1;
while (fibM > 1) {
int i = minimum(offset + fibMMm2, n - 1);
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else
return i;
}
if (fibMMm1 && arr[offset + 1] == x)
return offset + 1;
return -1;
}
int main()
{
int arr[] = { 10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100, 235 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 235;
int ind = fibonacciSearch(arr, x, n);
if (ind >= 0)
cout << "Found at index: " << ind;
else
cout << x << " isn't present in the array" ;
return 0;
}
|
C
#include <stdio.h>
int minimum( int x, int y) { return (x <= y) ? x : y; }
int fibonacciSearch( int arr[], int x, int n)
{
int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = -1;
while (fibM > 1) {
int i = minimum(offset + fibMMm2, n - 1);
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else
return i;
}
if (fibMMm1 && arr[offset + 1] == x)
return offset + 1;
return -1;
}
int main( void )
{
int arr[] = { 10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100, 235 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 235;
int index = fibonacciSearch(arr, x, n);
if (index >= 0)
printf ( "Element found at index: %d" , index);
else
printf ( "%d is not present in the array" , x);
return 0;
}
|
Java
import java.util.*;
class FibonacciSearch {
public static int minimum( int x, int y)
{
return (x <= y) ? x : y;
}
public static int fibonacciSearch( int arr[], int x,
int n)
{
int fibMMm2 = 0 ;
int fibMMm1 = 1 ;
int fibM
= fibMMm2 + fibMMm1;
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = - 1 ;
while (fibM > 1 ) {
int i = minimum(offset + fibMMm2, n - 1 );
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else
return i;
}
if (fibMMm1 == 1 && arr[n - 1 ] == x)
return n - 1 ;
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 10 , 22 , 35 , 40 , 45 , 50 ,
80 , 82 , 85 , 90 , 100 , 235 };
int n = 12 ;
int x = 235 ;
int index = fibonacciSearch(arr, x, n);
if (index >= 0 )
System.out.print( "Element found at index: "
+ index);
else
System.out.print(
x + " isn't present in the array" );
}
}
|
Python3
def fibonacci_search(arr, x, n):
fibMMm2 = 0
fibMMm1 = 1
fibM = fibMMm2 + fibMMm1
while fibM < n:
fibMMm2 = fibMMm1
fibMMm1 = fibM
fibM = fibMMm2 + fibMMm1
offset = - 1
while fibM > 1 :
i = min (offset + fibMMm2, n - 1 )
if arr[i] < x:
fibM = fibMMm1
fibMMm1 = fibMMm2
fibMMm2 = fibM - fibMMm1
offset = i
elif arr[i] > x:
fibM = fibMMm2
fibMMm1 = fibMMm1 - fibMMm2
fibMMm2 = fibM - fibMMm1
else :
return i
if fibMMm1 and arr[n - 1 ] = = x:
return n - 1
return - 1
arr = [ 10 , 22 , 35 , 40 , 45 , 50 ,
80 , 82 , 85 , 90 , 100 , 235 ]
n = len (arr)
x = 235
index = fibonacci_search(arr, x, n)
if index > = 0 :
print ( "Element found at index:" , index)
else :
print (x, "isn't present in the array" )
|
C#
using System;
class GFG {
public static int Min( int x, int y)
{
return (x <= y) ? x : y;
}
public static int FibonacciSearch( int [] arr, int x,
int n)
{
int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = -1;
while (fibM > 1) {
int i = Min(offset + fibMMm2, n - 1);
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else
return i;
}
if (fibMMm1 == 1 && arr[n - 1] == x)
return n - 1;
return -1;
}
public static void Main()
{
int [] arr = { 10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100, 235 };
int n = 12;
int x = 235;
int index = FibonacciSearch(arr, x, n);
if (index >= 0)
Console.Write( "Element found at index: "
+ index);
else
Console.Write(x
+ " isn't present in the array" );
}
}
|
Javascript
function fibonacciSearch(arr, x, n) {
let fibMMm2 = 0;
let fibMMm1 = 1;
let fibM = fibMMm2 + fibMMm1;
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
let offset = -1;
while (fibM > 1) {
let i = Math.min(offset + fibMMm2, n - 1);
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else return i;
}
if (fibMMm1 && arr[n - 1] == x) {
return n - 1;
}
return -1;
}
let arr = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100, 235];
let n = arr.length;
let x = 235;
let index = fibonacciSearch(arr, x, n);
if (index >= 0) {
document.write( "Element found at index: " + index);
} else {
document.write(x + " isn't present in the array" );
}
|
PHP
<?php
function fibonacciSearch( $arr , $x , $n )
{
$fibMMm2 = 0;
$fibMMm1 = 1;
$fibM = $fibMMm2 + $fibMMm1 ;
while ( $fibM < $n )
{
$fibMMm2 = $fibMMm1 ;
$fibMMm1 = $fibM ;
$fibM = $fibMMm2 + $fibMMm1 ;
}
$offset = -1;
while ( $fibM > 1)
{
$i = min( $offset + $fibMMm2 , $n - 1);
if ( $arr [ $i ] < $x )
{
$fibM = $fibMMm1 ;
$fibMMm1 = $fibMMm2 ;
$fibMMm2 = $fibM - $fibMMm1 ;
$offset = $i ;
}
elseif ( $arr [ $i ] > $x )
{
$fibM = $fibMMm2 ;
$fibMMm1 = $fibMMm1 - $fibMMm2 ;
$fibMMm2 = $fibM - $fibMMm1 ;
}
else
return $i ;
}
if ( $fibMMm1 == 1 && $arr [ $n - 1] == $x )
return $n - 1;
return -1;
}
$arr = array (10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100, 235);
$n = count ( $arr );
$x = 235;
$index = fibonacciSearch( $arr , $x , $n );
if ( $index >= 0)
printf( "Element found at index: " . $index );
else
printf( $x . " isn't present in the array" );
?>
|
Output
Found at index: 11
Time Complexity: O(log(n)) Using Fibonacci Search
Auxiliary Space: O(1) Using Fibonacci Search
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...