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.
16. Write “Item not Found”.
17. [End of If Structure.]
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.
- Algorithm to Find Maximum of Two Numbers
- Algorithm to Find Maximum of Three Numbers
- Algorithm to Input percentage From user and Print grade
- Algorithm to Print the Series 1 to N using for loop
- Algorithm to Print the Series 1 to N using while loop
- Algorithm to Print the Series 1 to N using Do While loop
- Algorithm to Reverse a given Number
- Algorithm to Find Whether a Number is Prime or Not
- Algorithm to Calculate Factorial using Function