Binary Search - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Binary Search

Description:

Computer Programming I. Dr. Tim Margush. Mathematics, Science and Technology Department ... Computer Programming I. Slide 4. Eliminating Unnecessary Work ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 11
Provided by: timma8
Category:

less

Transcript and Presenter's Notes

Title: Binary Search


1
Binary Search
  • Computer Programming I
  • Dr. Tim Margush
  • Mathematics, Science and Technology Department
  • Farquhar Center - Nova Southeastern University

2
Fast Searching
  • Binary Search is another algorithm used to search
    for a target in a list
  • The list must be sorted
  • You must be able to efficiently access the list
    in any order (random access)
  • Binary search repeatedly eliminates half of the
    remaining items from consideration

3
Eliminating Unnecessary Work
  • Knowing the target and that the list is sorted
  • Locate the middle item
  • If target is bigger, then it cannot be in the
    first half of the list
  • If the target is smaller, then it cannot be in
    the last half of the list

Little Items
Target
Middle Item
Big Items
4
Eliminating Unnecessary Work
  • Compare Target to the middle item
  • If target is bigger, then it cannot be in the
    first half of the list
  • If the target is smaller, then it cannot be in
    the last half of the list

Little Items
X
Target
Middle Item
Big Items
5
Eliminating Unnecessary Work
  • Compare Target to the middle item
  • If target is bigger, then it cannot be in the
    first half of the list
  • If the target is smaller, then it cannot be in
    the last half of the list

Little Items
Target
Middle Item
Big Items
X
6
Eliminating Unnecessary Work
  • Compare Target to the middle item
  • If target matches, the item is found and the
    search stops

Little Items
!
Middle Item
Target
Big Items
7
Eliminating Unnecessary Work
  • If target matches, the item is found and the
    search stops
  • Otherwise the search strategy is repeated on the
    "hopeful" list

Little Items
X
Middle Item
Target
Small Items
Middle Item
Big Items
8
Binary Search
  • //search for t in vector v of n items
  • int low0, highn-1, mid
  • bool found false
  • while (!found lowlthigh)
  • mid (lowhigh)/2
  • if (tvmid) found true
  • else if (tltvmid) highmid-1
  • else low mid1

9
Sequential vs. Binary Search
  • Sequential search eliminates 1 item with each
    comparison
  • An n-item list may require n comparisons
  • Binary search eliminates half of the items with
    each comparison
  • An n-item list requires only about lg(n)
    comparisons

10
Starting with 1000 items
  • With each comparison
  • 999 remain
  • 998 remain
  • 997 remain
  • 996 remain
  • 995 remain
  • 994 remain
  • 993 remain
  • (this may take a while)
  • With each comparison
  • 500 remain (or 499)
  • 250 remain (similarly)
  • 62 remain
  • 31 remain
  • 15 remain
  • 7 remain
  • 3 remain
  • 1 remains ltltltdone!
Write a Comment
User Comments (0)
About PowerShow.com