Algorithm to implement linear search for Data Structure

March 19, 2014 0 Comments

Algorithm to implement linear search:

LINEAR SEARCH (arr, n, key)
[ ‘arr’ is one-dimensional array of size ‘n’, key is element to search ]
success <-- FALSE
[Assuming the lower bound of the array as 1]
Repeat While (I<=n) AND (success = FALSE)
  If (arr[I] = key) Then:
    success <--TRUE
  [End of If]
[End of While]
Return success
     In this algorithm initially we assume that the search is unsuccessful by initializing a variable success as FALSE. Then we start the comparison of key from the first element by initializing the index variable I with 1. If the key is equal to first element then success is assigned with TRUE, which terminates the while loop. When the while loop is terminated the algorithm returns success as either TRUE or FALSE depending on the assignment. Similar comparisons are done with all the elements of the array by incrementing the index variable.

Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google