Computer Applications

How does the linear search find an element in the array? Explain your answer with a suitable example.

Java Arrays

8 Likes

Answer

In linear search, we start at the first element of the array and sequentially check each element of the list for the search value until a match is found or all the elements have been searched. As soon as the search value is found, the algorithm quits and returns the position (index) of the target value in the array.

For example, consider the following array:

int arr[] = {1, 8, 4, 7, 5};

We want to check if 7 is present in the array or not. Linear search will first check if 1 is equal to 7, then it will move on to the next element which is 8. It will keep doing this in a linear progression and when it reaches the element at index 3, it finds a match so it will give us this index 3 which means that 7 is present at index 3 of array arr.

Answered By

5 Likes


Related Questions