In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element.
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)
Search, Insert, and Delete in an Unsorted Array | Array Operations
In this post, a program to search, insert, and delete operations in an unsorted array is discussed.
Contact Us