Search an element in an unsorted array using minimum number of comparisons
Last Updated :
29 Mar, 2024
Given an array of n distinct integers and an element x. Search the element x in the array using minimum number of comparisons. Any sort of comparison will contribute 1 to the count of comparisons. For example, the condition used to terminate a loop, will also contribute 1 to the count of comparisons each time it gets executed. Expressions like while (n) {n–;} also contribute to the count of comparisons as value of n is being compared internally so as to decide whether or not to terminate the loop.
Examples:
Input : arr[] = {4, 6, 1, 5, 8},
x = 1
Output : Found
Input : arr[] = {10, 3, 12, 7, 2, 11, 9},
x = 15
Output : Not Found
Asked in Adobe Interview
Below simple method to search requires 2n + 1 comparisons in worst case.
for (i = 0; i < n; i++) // Worst case n+1
if (arr[i] == x) // Worst case n
return i;
How to reduce number of comparisons?
The idea is to copy x (element to be searched) to last location so that one last comparison when x is not present in arr[] is saved.
Algorithm:
search(arr, n, x)
if arr[n-1] == x // 1 comparison
return "true"
backup = arr[n-1]
arr[n-1] = x
for i=0, i++ // no termination condition
if arr[i] == x // execute at most n times
// that is at-most n comparisons
arr[n-1] = backup
return (i < n-1) // 1 comparison
C++
#include <bits/stdc++.h>
using namespace std;
string search( int arr[], int n, int x)
{
if (arr[n - 1] == x)
return "Found" ;
int backup = arr[n - 1];
arr[n - 1] = x;
for ( int i = 0;; i++) {
if (arr[i] == x) {
arr[n - 1] = backup;
if (i < n - 1)
return "Found" ;
return "Not Found" ;
}
}
}
int main()
{
int arr[] = { 4, 6, 1, 5, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 1;
cout << search(arr, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String search( int arr[], int n, int x)
{
if (arr[n - 1 ] == x)
return "Found" ;
int backup = arr[n - 1 ];
arr[n - 1 ] = x;
for ( int i = 0 ;; i++) {
if (arr[i] == x) {
arr[n - 1 ] = backup;
if (i < n - 1 )
return "Found" ;
return "Not Found" ;
}
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 1 , 5 , 8 };
int n = arr.length;
int x = 1 ;
System.out.println(search(arr, n, x));
}
}
|
Python3
def search(arr, n, x):
if (arr[n - 1 ] = = x) :
return "Found"
backup = arr[n - 1 ]
arr[n - 1 ] = x
i = 0
while (i < n) :
if (arr[i] = = x) :
arr[n - 1 ] = backup
if (i < n - 1 ):
return "Found"
return "Not Found"
i = i + 1
arr = [ 4 , 6 , 1 , 5 , 8 ]
n = len (arr)
x = 1
print (search(arr, n, x))
|
C#
using System;
class GFG {
static String search( int [] arr, int n, int x)
{
if (arr[n - 1] == x)
return "Found" ;
int backup = arr[n - 1];
arr[n - 1] = x;
for ( int i = 0;; i++) {
if (arr[i] == x) {
arr[n - 1] = backup;
if (i < n - 1)
return "Found" ;
return "Not Found" ;
}
}
}
public static void Main()
{
int [] arr = { 4, 6, 1, 5, 8 };
int n = arr.Length;
int x = 1;
Console.WriteLine(search(arr, n, x));
}
}
|
PHP
<?php
function search( $arr , $n , $x )
{
if ( $arr [ $n - 1] == $x )
return "Found" ;
$backup = $arr [ $n - 1];
$arr [ $n - 1] = $x ;
for ( $i = 0; ; $i ++)
{
if ( $arr [ $i ] == $x )
{
$arr [ $n - 1] = $backup ;
if ( $i < $n - 1)
return "Found" ;
return "Not Found" ;
}
}
}
$arr = array ( 4, 6, 1, 5, 8 );
$n = sizeof( $arr );
$x = 1;
echo (search( $arr , $n , $x ));
?>
|
Javascript
<script>
function search(arr, n, x)
{
if (arr[n - 1] == x)
return "Found" ;
let backup = arr[n - 1];
arr[n - 1] = x;
for (let i = 0;; i++) {
if (arr[i] == x) {
arr[n - 1] = backup;
if (i < n - 1)
return "Found" ;
return "Not Found" ;
}
}
}
let arr = [ 4, 6, 1, 5, 8 ];
let n = arr.length;
let x = 1;
document.write(search(arr, n, x));
</script>
|
Output:
Found
Time Complexity: O(n)
Auxiliary Space: O(1)
Number of Comparisons: Atmost (n+2) comparisons
Share your thoughts in the comments
Please Login to comment...