Linear search algorithm & Java program

Linear search

Linear search is one of the basic search techniques that we've now. Although this is not a very good search technique, one should understand this concept. Let's consider our aim to search for a key element in an array of elements. We loop through all the array elements and check for existence of the key element. Since we go element by element, this search is called as Linear search or sequential search. Search element is called as key element.

Linear search algorithm

BEGIN DECLARE key, array, i, found ASSIGN values to array/ACCEPT array values PRINT "Please enter key element:" ACCEPT key ASSIGN i with 1 FOR EACH i in 1 to array.length LOOP IF array[i] = key THEN ASSIGN found with true END IF END LOOP IF found = true THEN PRINT "Key found" ELSE PRINT "Key not found" END IF END

The above algorithm will search for a key even after key is found until all the array of elements are being checked. Once we identify the element, we don't need continue further. i.e.; we can break the loop and continue further. Here the Java program for the linear search