Linear Search in Data Structure
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
Introduction
 Linear search is a very simple search algorithm.
 In this type of search, a sequential search is made over
all items one by one.
 Every item is checked and if a match is found then that
particular item is returned, otherwise the search
continues till the end of the data collection.
Implementing Linear Search
 Following are the steps of implementation that we will
be following:
 Traverse the array using a for loop.
 In every iteration, compare the target value with the
current value of the array.
Cont..
 If the values match, return the current index of the
array.
 If the values do not match, move on to the next array
element.
 If no match is found, return -1.
Example
Algorithm
 Linear Search ( Array A, Value x)
 Step 1: Set i to 1
 Step 2: if i > n then go to step 7
 Step 3: if A[i] = x then go to step 6
Cont..
 Step 4: Set i to i + 1
 Step 5: Go to Step 2
 Step 6: Print Element x Found at index i and go to step
8
 Step 7: Print element not found
 Step 8: Exit
Assignment
 Explain linear search in DS with suitable example and
also discuss algorithm.

Linear search in ds

  • 1.
    Linear Search inData Structure Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    Introduction  Linear searchis a very simple search algorithm.  In this type of search, a sequential search is made over all items one by one.  Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
  • 3.
    Implementing Linear Search Following are the steps of implementation that we will be following:  Traverse the array using a for loop.  In every iteration, compare the target value with the current value of the array.
  • 4.
    Cont..  If thevalues match, return the current index of the array.  If the values do not match, move on to the next array element.  If no match is found, return -1.
  • 5.
  • 6.
    Algorithm  Linear Search( Array A, Value x)  Step 1: Set i to 1  Step 2: if i > n then go to step 7  Step 3: if A[i] = x then go to step 6
  • 7.
    Cont..  Step 4:Set i to i + 1  Step 5: Go to Step 2  Step 6: Print Element x Found at index i and go to step 8  Step 7: Print element not found  Step 8: Exit
  • 8.
    Assignment  Explain linearsearch in DS with suitable example and also discuss algorithm.