algorithm for linear search in array - data structures and algorithms
September 26, 2022

Algorithm for Linear Search in Array

In this tutorial, we are going to write a Linear search algorithm in Array. using this algorithm we can write a program to linear search in the array for an element in almost every programming language like java, python, c++, and c programming.


Algorithm for Linear Search in Array.

1. Read N
2. Repeat step3 for I:=0 to n-1
3.         Read A[I].
4. [End of Step 2. loop]
5. Read ITEM.
6. Set FOUND:=FALSE
7. Repeat step7 For I:=0 to n-1 [Searching]
8.       If A[I]=ITEM, then:
9.           a. Set FOUND:=TRUE.
10.           b. GoTo Step 8.
11.       [End of If Structure.]
12. [End of Step 6. loop]
13. If FOUND=TRUE, then:
14.       Write “Item is found at position:”,I+1.
15. Else:
16.       Write “Item not Found”.
17. [End of If Structure.]
18. Exit.

Here in the above algorithm first we read an Integer variable N that is presenting the length of an array. after that using the value of N we run a for loop to repeat step number 3. using step 3 we read the value from the user and store it in the array. and then read a new number that the user wants to search in the array. after that, we set the value of FOUND to FALSE and then using the value of N we again run a for loop and then check if the value of the ITEM variable is equal to the A[I].

then we set the value of FOUND variable to true else we again repeat the same steps 8 and 9 and then after ending the for loop we check if the value of FOUND variable is TRUE then we write “Item is found at position” on output screen else we write “Item not Found” on output screen and then we exit from the program.

Leave a Reply