ALGORITHM :(Linear Search) LSEARCH(Arr,N,POS,ITEM)
Here Arr is a Linear array with N elements, ITEM is the element to be searched and this algorithm find the position POS of the element ITEM .
1.[Initialize Counter] Set I=0 and POS=-1.
2.Repeat steps 3 and 4 while I !=N and POS=-1.
3. If Arr[I]=ITEM,
then: [Position Found ]Set POS=I+1 and Exit.
4.[Increase Counter]Set I=I+1.
[End Of Loop]
5. If POS=-1, then:
print element not found.
Else:
print element found at POS.
6.Exit
ALGORITHM :(Binary Search) BSEARCH(Arr,N,POS,ITEM)
Here Arr is a Linear array with N elements, ITEM is the element to be searched and this algorithm find the position POS of the element ITEM .Also L is lower bound ,U is upper bound and MID is the middle term between U and L.Here POS=-1 means element not found.
1.[Initialize Variables]Set L=0, U=N-1, MID=INT((U+L)/2), POS=-1.
2.Repeat steps 3 and 4 while L<=U and Arr[MID]!=ITEM.
3. If ITEM<Arr[MID], then:
Set U=MID-1.
Else If ITEM>Arr[MID], then:
Set L=MID+1.
Else :
Set POS=MID+1 and goto step 5.
4.Set MID=(U+L)/2.
[End Of Loop]
5.Exit
2.Repeat steps 3 and 4 while L<=U and Arr[MID]!=ITEM.
3. If ITEM<Arr[MID], then:
Set U=MID-1.
Else If ITEM>Arr[MID], then:
Set L=MID+1.
Else :
Set POS=MID+1 and goto step 5.
4.Set MID=(U+L)/2.
[End Of Loop]
5.Exit
If you have any suggestions regarding this post, please comment below in the comment section.
No comments