Searching Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Searching Arrays

Description:

after 1st compare, throw away half, leaving 500 elements to be searched. after 2nd compare, throw away half, leaving 250. Then 125, 63, 32, 16, 8, 4, 2, 1 are left. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 9
Provided by: defau640
Learn more at: https://www.cs.uno.edu
Category:

less

Transcript and Presenter's Notes

Title: Searching Arrays


1
Searching Arrays
  • ANSI-C

2
Searching
  • Searching looking for something
  • Searching an array is particularly common
  • Goal determine if a particular value is in the
    array
  • If the entries in the array are not in order
  • start at the beginning and look at each
  • element to see if it matches

3
Linear Search
  • / If x appears in table0..length-1, return its
  • position in the table, i.e.,
  • return k so that tablekentry. If entry not
  • found, return -1
  • /
  • int lsearch (int table , int length, int entry)
  • int index 0
  • while (index lt length tableindex ! entry)
    index index 1
  • if (index gt length )
  • index -1
  • return index

4
Binary search
  • Can we do better?
  • "Binary search" works if the array is sorted
  • The strategy in this case is
  • 1. Look for the target in the middle.
  • 2. If you dont find it, you can ignore half of
    the array, and repeat the process with the other
    half.
  • Example Find first page of Pizza
  • listings in the yellow pages

5
Binary Search
  • / If entry appears in table0..length-1, return
    its
  • location, i.e. return k so that
    tablekentry.
  • If entry not found, return -1
  • /
  • int bsearch (int table , int length, int entry)
  • int left
  • int right
  • int middle
  • while ( _______________ )
  • middle (left right) / 2
  • if (tablemiddle lt entry)
  • left middle 1
  • else
  • right middle - 1
  • _________________

6
Binary Search
  • / If entry appears in table0..length-1, return
    its
  • location, i.e. return k so that
    tablekentry.
  • If entry not found, return -1
  • /
  • int bsearch (int table , int length, int entry)
  • int left 0
  • int right length 1
  • int middle
  • while ( left lt right)
  • middle (left right) / 2
  • if (tablemiddle lt entry)
  • left middle 1
  • else
  • right middle - 1
  • _________________

7
Binary Search
  • / If entry appears in table0..length-1, return
    its
  • location, i.e. return k so that
    tablekentry.
  • If entry not found, return -1
  • /
  • int bsearch (int table , int length, int entry)
  • int left 0
  • int right length 1
  • int middle
  • int k -1
  • while ( left lt right)
  • middle (left right) / 2
  • if (tablemiddle lt entry)
  • left middle 1
  • else
  • right middle - 1
  • if ( left lt length tableleft entry )
  • k left
  • return k

8
Is it worth the trouble?
  • Suppose you had 1000 elements
  • Linear search would require maybe 500 comparisons
    on average
  • Binary search
  • after 1st compare, throw away half, leaving 500
    elements to be searched.
  • after 2nd compare, throw away half, leaving 250.
  • Then 125, 63, 32, 16, 8, 4, 2, 1 are left.
  • After at most 10 steps, youre done!
  • What if you had 1,000,000 elements??
Write a Comment
User Comments (0)
About PowerShow.com