Linear Search
Linear Search is defined as a sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found, otherwise the search continues till the end of the data set. It is the easiest searching algorithm.
Linear Search Algorithm
Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in the array.
Step 3: If both are matched, display “Target element is found” and terminate the Linear Search
function.
Step 4: If both are not matched, compare the search element with the next element in the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with the
last element of the array.
Step 6 – If the last element in the list does not match, the Linear Search Function will be
terminated, and the message “Element is not found” will be displayed.
Follow the given steps to solve the problem:
- Set the first element of the array as the current element.
- If the current element is the target element, return its index.
- If the current element is not the target element and if there are more elements in the array, set the current element to the next element and repeat step 2.
- If the current element is not the target element and there are no more elements in the array, return -1 to indicate that the element was not found.
No comments:
Post a Comment