Announcements - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Announcements

Description:

Now imagine doing the reverse: find the name of a business given just their phone number. ... Normally, when you search the phone book, you implicitly use the ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 23
Provided by: knightCi
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Reading for Thursday 14.3, 14.5 in 3rd Edition,
    19.3, 19.5 in 2nd Edition
  • Quiz on Thursday (your last quiz!)
  • Homework 10 due Friday (11/30) at 6pm
  • Skeleton code is due today (11/27) at 1159pm
  • There will be one more homework after this one
  • Last class is next Tuesday (12/4)
  • I will host a review session for the final on
    Wednesday (1240-230?)
  • Your final is the last day of the semester,
    Friday 12/14 at 2pm

2
Searching Algorithms
3
The Search Problem
  • Problem Statement
  • Given an ordered set of data (e.g., an array) and
    a particular piece of data,
  • Find the first occurrence of the particular piece
    of data in the ordered set.

4
The Search Problem
  • Problem Statement, revisited
  • Input
  • A set of data (an array, ArrayList, LinkedList,
    )
  • A single data element
  • Output
  • Position of the data element in the data set, or
  • -1 if the element does not appear in the data set

5
An Example Search Problem
  • Imagine flipping through the Yellow Pages,
    looking for a pizza place near you.
  • Its pretty easy you just flip to the section
    for P, then look for Pi, then Piz, ,
    Pizza.
  • Now imagine doing the reverse find the name of
    a business given just their phone number.
  • What algorithm will find the number in the phone
    book?

6
Linear Search
  • Input Array D, integer key
  • Output first index of key in D,
  • or -1 if not found
  • For i 0 to end of D
  • if Di equals key
  • return i
  • return -1

7
Linear Search for Phone Numbers
  • Input Array D of Business objects,
  • phone number key
  • Output first index where keys phone
  • number matches D, or -1 if not found

Business phone address name
For i 0 to end of D if Di.phone matches
key return i return -1
8
Exercise
  • Implement a class called Business that includes a
    static method for finding the index of a
    particular business in an array of Businesses
    using linear search.
  • Create fields for name, address, and phone number
  • Create a linearSearch() method that finds a
    business in an array, given the phone number

9
What happens if our array is huge?
  • Imagine finding a particular word on the Web
    (approximately 10,000,000,000 documents)
  • Linear search would take a long time
  • Two common faster search techniques are
  • Indexing (used on the Web and in databases)
  • Binary search
  • Well discuss binary search because its simpler,
    but you can learn about indexing in later CIS
    classes

10
Binary Search Normal Phone Book Use
  • Normally, when you search the phone book, you
    implicitly use the fact that its sorted.
  • Binary search does the same thing, and it only
    works if your data (array) is sorted.

11
Exercise
  • Modify your Business class so that the
    Arrays.sort() method will work on it
  • What are the two things you need to do?
  • Declare that the class implements the Comparable
    interface
  • Implement the compareTo() method
  • Make it sort the Businesses according to phone
    number

12
Binary Search Algorithm
  • Input Sorted Array D, integer key
  • Output first index of key in D, or -1 if not
    found
  • left 0, right index of last element
  • while left
  • middle index halfway between left, right
  • if Dmiddle matches key
  • return middle
  • else if key comes before Dmiddle // D is
    sorted
  • right middle -1
  • else
  • left middle 1
  • return -1

13
You guessed it Exercise
  • Implement a binary search method in your Business
    class

14
How much faster is binary search?
  • Way, way faster
  • Assuming the array is already sorted
  • But precisely how much?

15
Announcements
  • Reading for Tuesday 8.5 in 3rd Edition, 9.5 in
    2nd Edition
  • Homework 10 due Friday (11/30) at 6pm
  • Homework 11 due Friday (12/7) at 6pm
  • Last class is Tuesday (12/4)
  • Your final is the last day of the semester,
    Friday 12/14 at 2pm
  • I will host a review session for the final on
    Wednesday during lab (12/5, 1240-230) bring
    lots of questions!

16
Introduction to Computational Complexity
17
How complex is a computation?
  • Computational Complexity is the study of how fast
    programs are (or sometimes how much memory they
    use).
  • For example
  • Binary search on an array of size m takes about
    log2(m) steps.
  • Linear search on an array of size m takes about m
    steps.
  • Also
  • BubbleSort on an array of size m takes about m2
    steps.
  • MergeSort on an array of size m takes about m
    log2(m) steps.

18
What do you mean, about?
  • Comp. Scientists study how fast things are
    approximately.
  • For example,
  • Algorithm 1 takes 2 m steps
  • Algorithm 2 takes 3 m 40 steps
  • (for any array of size m)
  • Then we say Algorithm 1 and 2 are both about the
    same!

19
Moores Law
  • Manufacturers double the speed of computers
    roughly every 1.5 years.
  • This has held true since the first computers
    (1950)
  • So a factor of 2 difference in an algorithm
    doesnt really matter so much just wait a year!

20
(No Transcript)
21
Defining approximately
  • We say f(m) O(g(m)) if
  • There are constants a and M such that
  • This is called Big-O notation
  • e.g., 3m 40 O(m)
  • (read Big-O of m or O of m)
  • Likewise, 2m O(m)
  • So, 3m 40 and 2m are basically the same

22
Complexity Classes
  • The Big-O notation helps us group algorithms into
    classes with similar speed
  • For example, MergeSort and QuickSort both belong
    to the class with speed O(m log2(m))
  • Common classes of algorithms
Write a Comment
User Comments (0)
About PowerShow.com