Javatpoint Logo
Javatpoint Logo

Linear search in C

Finding pieces within a collection is a typical task in the world of programming. The linear search is one of the most elementary and basic search methods. The specifics of the linear search will be covered in this blog post, along with its implementation in the C programming language, syntax examples, and samples of the expected result. So, let's get going!

The fundamental search technique known as linear search that scans every element in a collection until the required element is discovered or the collection has been thoroughly searched. It is often referred to as sequential search. When dealing with short or unordered lists, this algorithm is especially helpful.

The linear search algorithm works in the following straightforward manner:

  1. Starting from the collection's commencement.
  2. Compared with the target element, compare the current element.
  3. Return the element's index or location if a match is discovered.
  4. Return the appropriate indication (e.g., -1) if the collection's conclusion is reached without a match.

We need an array or list of elements to look through in order to execute linear search in C. Let's take an example to understand how linear search work:


Assuming that the target element is 8, let's use the example array [10, 2, 8, 5, 17]. Running the code yields the following result:

Element found at index: 2

The element '8' was found in this instance by the linear search technique at index 2 of the array.

Let's dissect the implementation process in detail:

  1. To use the printf function, we include the h library.
  2. The array arr[], the array's size n, and the element to be searched are the three arguments provided to the linearSearch function.
  3. We create the integer variable i and use a for loop to iterate through the array.
  4. We compare each element of the array with the target element inside the loop.
  5. If a match is discovered, we return the element's location's index.
  6. We return -1 to denote that the element was not found if, after searching the full array, no matches were discovered.
  7. We declare an array called arr[] with a few random members in the main function.
  8. By dividing the array's overall size by the size of a single member, we may get the array's n-dimensional size.
  9. The target element we wish to look for is specified (in this example, 8).
  10. The array, size, and target are passed as arguments when we use the linearSearch function, and we store the result in the result variable.
  11. Finally, we evaluate the value of the outcome. We print a message explaining that the element was not found if it is -1. If not, we display the index at which the element was located.


Let's sum up by saying that linear search is a fundamental technique for locating items in a collection and that it offers a fundamental framework for programming search tasks. Even though it might not be the most effective method for handling huge or sorted data sets, linear search is a useful place for beginners to start when learning about searching algorithms.

We can progressively scan through an array or list in C, comparing each element with the target element, looking for matches until a match is found or the collection is exhausted. Even those with no prior programming experience can easily grasp and use the technique due to its simplicity.

Although linear search is the best option for small or unordered lists, it might not be the best option for bigger data sets. For sorted or organized collections, more advanced search algorithms with shorter search durations include binary search and hash-based methods. However, as it serves as the basis for more intricate algorithms, knowing the idea of linear search is essential.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA