Javatpoint Logo
Javatpoint Logo

C program to Search an Element in an Array

In this article, we will discuss the C program to search for an element in an Array with their different ways and examples.

What is an Array?

A data structure called an array holds a fixed-length series of identical-type items. It is frequently used to store and manipulate data collections because indexing enables efficient access.

Ex: intnumbers[] = {10, 20, 30, 40, 50};

Searching an Element in an Array

A typical operation in computer programming is looking for a particular element in an array. The efficiency of your code may be greatly improved by using efficient searching algorithms whether you are searching for the existence of a certain value locating the index of an element, or verifying if an element exists. The many methods for searching for elements in an array using the C programming language will be discussed in this article.

There are mainly two ways to Search an Element in an Array:

1. Linear Search

A straightforward search strategy used to locate a given element in an array or list is called linear search, sometimes referred to as sequential search. It operates by comparing each array member to the target value to find a match or traverse the full array iteratively.

The fundamental steps in linear search are as follows:

  1. Start with the array's topmost elements.
  2. The target value should be compared to the current element.
  3. The search is successful if the current element matches the requested value, and then the algorithm can return the element's index or any other desired output.
  4. Go to the following element in the array if the current element does not match the desired value.
  5. Until a match is made or the end of the array is reached, repeat steps 2-4.

Program:

Output:

An element found at index 2

2. Binary Search

The binary search technique is utilized to quickly locate a specific element in a sorted array or list. It uses a divide-and-conquer strategy, periodically cutting the search area in half until the target element is located or found to be absent.

This is how binary search functions:

  1. Have a sorted array or list as a base.
  2. Establish two pointers, left and right, with their initial values pointing to the array's first and end members.
  3. Use (left + right) / 2 to get the index of the center element.
  4. Compare the target value to the middle element.
    1. The search is successful if they are equal, and then the program can return the index or any other required result.
    2. The right pointer should be moved to the element preceding the middle element if the middle element is greater than the target value.
    3. Move the left pointer to the element following the middle element if the middle element's value is less than the target value.
  5. Steps 3 and 4 should be repeated until the target element is located or the left pointer exceeds the right pointer.
  6. The desired element is not in the array if it cannot be located.

Program:

Output:

An element found at index 4






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA