Searching Algorithm
Linear Search
• find whether a given number is
present and if it is present then at
what location it occurs.
• There are two types of searching
algorithm present in data
structure
– Linear search or sequential
search
– Binary search
Linear Search
Linear Search
Pros
Very easy to understand
and implement
Quick to write the code
Ideal for Unsorted array
Ideal for Array with lesser
number of items
Cons
Time complexity is bad
O(n)
Binary Search gives better,
efficient and faster
results
Binary Search
• Binary search is a very
fast and efficient
searching algorithm. It
requires to be list be in
sorted order, ie; either
in ascending or
descending.
How Binary Search works
• The array needs to be sorted in either
ascending or descending order
• In our case we are taking an example
for an array sorted in ascending
order.
• The searching algorithm proceed
from any of two halves
• Depends upon whether the element
you are searching is greater or
smaller than the central element
• If the element is small then,
searching is done in first half
• If it is big then searching is done in
second half.
• It is fast search algorithm with the
complexity of O(log n).
Implementation of Binary Search
• Take a sorted array
(mandatory)
• Find mid using formula m =
(l+r)/2
• If the item to be searched is
greater than mid
– Check the right subarray
• If the item to be searched is
lesser than the mid
– Check the left subarray
• If mid element == item return
with the position where found
• Else keep doing the above
steps until you violate the
bounds of the array
Methods Discussed
• Recursive Approach • Iterative Approach
Searching Algorithm.pptx

Searching Algorithm.pptx

  • 1.
  • 2.
    Linear Search • findwhether a given number is present and if it is present then at what location it occurs. • There are two types of searching algorithm present in data structure – Linear search or sequential search – Binary search
  • 3.
  • 4.
    Linear Search Pros Very easyto understand and implement Quick to write the code Ideal for Unsorted array Ideal for Array with lesser number of items Cons Time complexity is bad O(n) Binary Search gives better, efficient and faster results
  • 5.
    Binary Search • Binarysearch is a very fast and efficient searching algorithm. It requires to be list be in sorted order, ie; either in ascending or descending.
  • 6.
    How Binary Searchworks • The array needs to be sorted in either ascending or descending order • In our case we are taking an example for an array sorted in ascending order. • The searching algorithm proceed from any of two halves • Depends upon whether the element you are searching is greater or smaller than the central element • If the element is small then, searching is done in first half • If it is big then searching is done in second half. • It is fast search algorithm with the complexity of O(log n).
  • 7.
    Implementation of BinarySearch • Take a sorted array (mandatory) • Find mid using formula m = (l+r)/2 • If the item to be searched is greater than mid – Check the right subarray • If the item to be searched is lesser than the mid – Check the left subarray • If mid element == item return with the position where found • Else keep doing the above steps until you violate the bounds of the array
  • 8.
    Methods Discussed • RecursiveApproach • Iterative Approach