Searching in Data Structure

Searching

A process of finding an element from list of element stored any order/randomly. Searching is of two types

  1. Linear search
  2. Binary search
  • Linear search

Steps

  1. Each element of the array read one by one segmentally & compared with the desired element.
  2. Successful-element available
  3. Unsuccessful-all element is read and the desired element not found.
  4. n-no. of element array
sequential search

Linear Search is also known as sequential search, elements of the array are searched sequentially.

 Algorithm
Requirement->

n->no. of element in array.

Element-> element to be searched.

  1. Input an array a of n element and input element to be searched.
  2. Initialize i=0 and Repeat through step 3 if(i
  3. If (element== a[i]) , print “successful search”
  4. If i>=n) , print(“Unsuccessful search”)

A technique that search an item by ordered set of item is known binary search

Search an item
Note: Elements are in ordered set

binary search case

Efficient method

  1. find the middle element of the array
  2. Compare the middle element with data to be searched

desired element found successful Search

< search first half

> search second half

Working

working of binary search

Algorithm

  1. Input an array a and n items, the element is to be searched.
  2. Start=0 end=n-1 mid=(start+end)/2
  3. Repeat step 4 and 5 while (start<=end) and (a[mid!=element)
  4. If(element>a[mid])
    start= mid+1
    else
    end= mid-1
  5. mid=(start+end)/2
  6. If a[mid]==element)
    print “Successful Search”
  7. If(start>end)
    print “Unsuccessful Search”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top