Linear search is the most fundamental and the simplest search method. It is an exhaustive searching technique where every element of a given list is compared with the item to be searched (usually referred to as ‘key’). So, each element in the list is compared one by one with the key. This process continues until an element matching the key is found and we declare that the search is successful. If no element matches the key and we have traversed the entire list, we declare the search is unsuccessful i.e., the key is not present in the list. This item by item comparison is done in the order, in which the elements are present in the list, beginning at the first element of the list and moving towards the last. Thus, it is also called sequential search or serial search. This technique is useful for collection of items that are small in size and are unordered.
Given a list numList of n elements and key value K, Algorithm uses a linear search algorithm to find the position of the key K in numList.
Algorithm : Linear Search
LinearSearch(numList, key, n)
Step 1: SET index = 0
Step 2: WHILE index < n, REPEAT
Step 3 Step 3: IF numlist[index]= key THEN
PRINT “Element found at position”, index+1
STOP
ELSE index =
index+1
Step 4: PRINT “Search unsuccessful”