You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Linear search (also called sequential search) is the simplest searching algorithm. It works by checking each item in a list, one by one, from the beginning to the end, until the desired item is found or the entire list has been searched.
The algorithm follows these steps:
FUNCTION linearSearch(list, target)
FOR i = 0 TO LENGTH(list) - 1
IF list[i] = target THEN
RETURN i
ENDIF
NEXT i
RETURN -1
ENDFUNCTION
The function returns the index (position) of the target if found, or -1 if the target is not in the list.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.