What is Linear Search Algorithm?

Algorithm: LINEAR (DATA, N, ITEM, LOC)

Here DATA is an array with N elements ITEM is a given item of information. The algorithm finds the location LOC of ITEM in DATA, or sets LOC := 0 if search is unsuccessful.

1.      [Insert ITEM at the end of DATA.] Set DATA [N+1] := ITEM.

2.      [Initialize counter] Set LOC := 1

3.      [Search for ITEM.]

Repeat while DATA [LOC] ITEM Set LOC = LOC+1

[End of loop.]

4.      [Successful] If LOC = N+1 then set LOC := 0

         5.    Exit

No comments

Powered by Blogger.