Linear Search


Linear Search

Searching is the process of looking for a certain item in a list. If the element is found in the list, the process is considered successful, and the location of that element is returned; otherwise, the search is considered unsuccessful. To search some items in the list, two prominent search strategies are extensively utilized. However, the algorithm chosen is determined by the list's organization.

  • Search in a straight line
  • Binary Lookup

Linear Search

Linear search, often known as sequential search, is the most basic search technique. In this type of search, we simply go through the entire list and match each element to the Item whose position has to be discovered. The algorithm delivers the Item's location; otherwise, NULL is returned.

Algorithm

  • LINEAR SEARCH (A, N, VAL)
  • Step 1: SET POS = -1 [INITIALIZE]
  • Step 2: SET I = 1 [INITIALIZE]
  • Step 3: While I=N, repeat Step 4
  • IF A[I] = VAL (Step 4)
  • I PRINT POS = SET POS
  • [END OF IF] Move on to Step 6.
  • [END OF LOOP] SET I = I + 1
  • Step 5: PRINT " VALUE IS NOT PRESENTING THE ARRAY " [END OF IF] IF POS = -1
  • EXIT is the sixth step.

Complexity of algorithm

Complexity Best Case Average Case Worst Case
Time O(1) O(n) O(n)
Space O(1)