Linear search is one of the easiest search algorithms which is used to search data (e.g Integer/Number) in a data structure (e.g Array). The principle of linear search is to start from the beginning of the array and match each array element with the search Item until the match is found.

Since we are linearly (one by one) searching an element in the array (or other data structure), So it is known as linear search.

Linear Search Algorithm

To search a number in the array using linear search technique in C:

  1. Take input in the array.
  2. Enter the number which needs to be searched in the array.
  3. Run a for loop from 0 to array length.
  4. Match every element.
  5. If found then print the “Found”, else output “Not Found”

C/Java Program of Linear Search

C

Java

We have assumed at the beginning of the search process that element is not present in the array i.e bool flag = false so that we can Output “Not Found” as default message. Or if the element is present in the array then the value of the flag is set to true in the loop i.e flag = true.

Output of Program

Linear Search in C

Linear Search C program Code

Linear search is not the best searching technique. For a huge array, if the search element is present at the beginning of the array then the result will be outputted instantly but if the search element is present at the very last of the array, then almost all element will be checked unnecessarily, Which is the worst case. Hence its time complexity is O(n) n is the size of the array.

That’s all for the linear search C program. If you have any doubt then comment below.

Leave a Reply

Close Menu