Search Operation

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++




// C++ program to implement linear
// search in unsorted array
#include <bits/stdc++.h>
using namespace std;
 
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
     
      // If the key is not found
    return -1;
}
 
// Driver's Code
int main()
{
    int arr[] = { 12, 34, 10, 6, 40 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Using a last element as search element
    int key = 40;
   
      // Function call
    int position = findElement(arr, n, key);
 
    if (position == -1)
        cout << "Element not found";
    else
        cout << "Element Found at Position: "
             << position + 1;
 
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C




// C program to implement linear
// search in unsorted array
#include <stdio.h>
 
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
     
      // If the key is not found
    return -1;
}
 
// Driver's Code
int main()
{
    int arr[] = { 12, 34, 10, 6, 40 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Using a last element as search element
    int key = 40;
   
      // Function call
    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




// Java program to implement linear
// search in unsorted arrays
 
class Main {
   
    // Function to implement
    // search operation
    static int findElement(int arr[], int n, int key)
    {
        for (int i = 0; i < n; i++)
            if (arr[i] == key)
                return i;
 
          // If the key is not found
        return -1;
    }
 
    // Driver's Code
    public static void main(String args[])
    {
        int arr[] = { 12, 34, 10, 6, 40 };
        int n = arr.length;
 
        // Using a last element as search element
        int key = 40;
       
          // Function call
        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




# Python program for searching in
# unsorted array
 
 
def findElement(arr, n, key):
    for i in range(n):
        if (arr[i] == key):
            return i
           
    # If the key is not found
    return -1
 
 
# Driver's code
if __name__ == '__main__':
    arr = [12, 34, 10, 6, 40]
    key = 40
    n = len(arr)
 
    # search operation
    index = findElement(arr, n, key)
    if index != -1:
        print("Element Found at position: " + str(index + 1))
    else:
        print("Element not found")
 
    # Thanks to Aditi Sharma for contributing
    # this code


C#




// C# program to implement linear
// search in unsorted arrays
using System;
 
class main {
    // Function to implement
    // search operation
    static int findElement(int[] arr, int n, int key)
    {
        for (int i = 0; i < n; i++)
            if (arr[i] == key)
                return i;
         
          // If the key is not found
        return -1;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 12, 34, 10, 6, 40 };
        int n = arr.Length;
 
        // Using a last element as
        // search element
        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));
    }
}
 
//  This code is contributed by vt_m.


Javascript




// Javascript program to implement linear
// search in unsorted array
 
 
// Function to implement search operation
function findElement( arr, n, key)
{
    let i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
 
    return -1;
}
 
 
     
    // Driver program
     
    let arr = [12, 34, 10, 6, 40];
    let n = arr.length;
 
    // Using a last element as search element
    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
// PHP program to implement linear
// search in unsorted array
 
// Function to implement
// search operation
function findElement($arr, $n, $key)
{
    $i;
    for ($i = 0; $i < $n; $i++)
        if ($arr[$i] == $key)
            return $i;
     
      // If the key is not found
    return -1;
}
 
// Driver Code
$arr = array(12, 34, 10, 6, 40);
$n = sizeof($arr);
 
// Using a last element
// as search element
$key = 40;
$position = findElement($arr, $n, $key);
 
if ($position == - 1)
    echo("Element not found");
else
    echo("Element Found at Position: " . ($position + 1));
 
// This code is contributed by Ajit.
?>


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.

Similar Reads

Search Operation:

In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element....

Insert Operation:

...

Delete Operation:

...

Contact Us