How Linear search in C Works? With Example

Linear search in C

Linear search in C is known as sequential search in the computer world that is used in C to find that a number is present in an array or not. If the number is found, then the location is checked where it occurs. It is the comparison of each element in the list with the required element until it is searched.

What is linear search in C:

The method of finding an element within a list is a sequential search that continues until the list ends. To know what is linear search in C, a user can take note of the following.

  • A linear search in C using function basically begins with the leftmost element of an array as arr[] and is then compared with each element in the list one by one.
  • If x is matching with an element, then it returns the index.
  • If it does not match with the elements on the list, then it returns as -1.

Linear search in array in C:

Linear Search in Array in C is implemented using the following steps.

  • The first step is to read the search element from the user.
  • The second step is to compare the element to be searched with the first element in that list.
  • If the element matches with the element in the list, then display “Given element is found” and then stop the execution of that function.
  • If the searched element doesn’t match, then it is continued to be matched with the next element on the list.
  • These processes are repeated again and again until the required element isn’t matched with any of the elements on the list or until the list ends.
  • If it fails to match the list element, then it can display “Element is not found” and then stop the execution of the function.

How Linear Search in C Works?

To understand the process of how a linear search works, we see the discussion in an example that is referenced below. Considering an element k = 1, here is a small example.

  • Start from the first element in the list by comparing the value k with the elements as x.
  • If x == k, which means that x matches with k, then return the index.
  • If it doesn’t match in the entire list, then return not found.

Algorithm for Linear Search in C

A small example of an algorithm for linear search in C is

LinearSearch(array, key)

for each item or key in the array

if item == element

return its index

Also Read: Merge Sort Algorithm in C Programming Language

Linear Search Complexities

Linear search in c complexities can be taken in 2 cases as linear search scans only one element or item at a time. It doesn’t jump to any sections.

  • There is the worst-case complexity in linear search complexity when the result is O(n), also known as O(n) search.
  • There is another complexity in which the time increases, which is taken to search the number of elements as they are increased.
Time Complexity: O(n)
Space Complexity: O(1)

Linear Search Applications

In most cases, the linear search program in C is most widely used for searching any kind of operations that are in smaller arrays. Considering an example of these arrays, the limit can be considered as (<100 items).

Example of linear search in c:

#include <iostream>

using namespace std;

int main() {

int blue, c, n=6;

int arr[] = { 12, 34, 68, 74, 165, 54};

blue=165;

for (c = 0; c < n; c++) {

if (arr[c] == blue) {

printf("%d is present at location %d.\n", search, c+1);

break;

}

}

if (c == n)

printf("%d isn't present in the array.\n", search);

return 0;

}

The time which is actually required to find out an element from the list using any linear search algorithm is greatly depending on the size of that list. When the required element to be searched is found in the beginning or in the first place, then it is considered as the best-case scenario.

If the element is not present at the beginning of the list and you find it at the end, which takes more time, it is considered a worst-case scenario.

Conclusion

This was a discussion on linear search in C. We discussed linear search in array in C as well as its algorithm with an example. For more details, you can leave a comment in the box given below.

FAQ’s

What is linear search in C++?

A linear search algorithm is a process or method that is used to find whether a given number or element to be search is present in an array or not. If it’s present, then the location is searched.

What is mean by linear search?

It is a sequential search process in computer science for finding an element within a list. If the element is found, a message is generated, if it doesn’t, the list ends, and another message is generated.

Add a Comment

You have to agree to the comment policy.

53  +    =  55