Skip to content

Search an element in an Array: {Linear Search} – Program and Algorithm

Search an element in an array

In this article, you’ll learn how to search an element in an array in C/C++, along with that you’ll also see the algorithm to search an element in an array.

So, Let’s get started!

Prerequisite: You should have the knowledge of Traverse Operation in Array.

What is search operation in array?

Search operation in Array or searching an element in an Array is the process of finding an element or the location of an element within an array.

Or,

In other words – It is the process of finding a desired element or the location of the desired element with a given key value, or finding the locations of all such elements which satisfy one or more conditions.

How do you search for an element in an array?

Let’s see how you can search an element in an array step-by-step:

Variables we are using here:

  1. a : Name of the array.
  2. n: Size of the array (i.e., Total number of elements in the array).
  3. i : Loop counter or counter variable for the for loop.
  4. x : The data element to be search.

The following algorithm search a data element x in a linear array a.

Algorithm to Search an element in an Array:

  • Step 01: Start
  • Step 02: [Initialize counter variable. ] Set i = 0
  • Step 03: Repeat Step 04 and 05 for i = 0 to  i < n
  • Step 04: if a[i] = x, then jump to step 07
  • Step 05: [Increase counter. ] Set i = i + 1
  • Step 06: [End of step 03 loop. ]
  • Step 07: Print x found at i + 1 position and go to step 09
  • Step 08: Print x not found (if a[i] != x, after all the iteration of the above for loop. )
  • Step 09: Stop

Explanation:

To search for a given ITEM (here, the ITEM is ‘x‘) in an array ‘a‘ we compare the ITEM ‘x‘ with each element of the array ‘a‘ one by one.

That is, we test whether a[0] = x, and then we test whether a[1] = x, and so on… up till we reach to location where the ITEM ‘x‘ first occurs in the array ‘a‘.

And, when we reach to our desired element’s location which we are searching for, then we print ” x found at i + 1 position” and then, the iteration of the for loop breaks and jump out of the loop. After that, the program exits with successful execution.

And, If after all iteration, when we don’t find our desired element then, we print “The element ‘x’ not found”. After that, the program exits with successful execution.

Visual Representation:

Implementation in C:

// writing a program in C to search an element in an array
 
#define MAX 50
int main()
{
     int i, n, x;
     int a[MAX];
     printf("Enter the size of the array: ");
     scanf("%d", &n);
     printf("Enter %d elements:\n", n);
     for(i=0;i<n;i++)
        scanf("%d", &a[i]);
     printf("Enter the element to search: ");
     scanf("%d", &x);
     for(i=0;i<n;i++)
     {
         if(a[i]==x)
            {printf("%d found at %d position.", x, i+1);
             return 0;}
     }
     printf("%d not found", x);
     return 0;
}

If you compile and run the above program, it will produce the following result:

Output:

# searching an element which is present in the array
 
Enter the size of the array: 10
Enter 10 elements:
10
14
19
26
27
31
33
35
42
44
Enter the element to search: 33
33 found at 7 position.
 
# searching an element which is not present in the array
 
Enter the size of the array: 10
Enter 10 elements:
10
14
19
26
27
31
33
35
42
44
Enter the element to search: 25
25 not found


Implementation in C++:

// writing a program in C++ to search an element in an array

#include <iostream>
#define MAX 50
using namespace std;
int main()
{
     int i, n, x;
     int a[MAX];
     cout << "Enter the size of the array: ";
     cin >> n;
     cout << "Enter " << n << " elements:\n";
     for(i=0;i<n;i++)
        cin >> a[i];
     cout << "Enter the element to search: ";
     cin >> x;
     for(i=0;i<n;i++)
     {

        if(a[i]==x)
            {cout << x << " found at " << i+1 << " position.";
            return 0;}
     }
     cout << x << " not found";
     return 0;
}

If you compile and run the above program, it will produce the following result:

Output:

# searching an element which is present in the array

Enter the size of the array: 5
Enter 5 elements:
11
22
33
-512
99
Enter the element to search: -512
-512 found at 4 position.


Conclusion:

In this article, you learned how to search an element in an array in C/C++, you also became familiar with the algorithm to search an element in an array.

So,

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.

And, and, and…

Don’t forget to share this article to your lovely friends.

Coz, as you know, Sharing is Caring! 💗… Right?


Share this Post

Leave a Reply

Your email address will not be published.