1 / 10

Lecture 11

Lecture 11. Searching Linear Search Binary Search recursive iterative polymorphic. Searching. One of the most common and important application areas The problem: Data is stored in a list of records, each of which includes a distinct identifying key

rrossi
Download Presentation

Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11 • Searching • Linear Search • Binary Search • recursive • iterative • polymorphic

  2. Searching • One of the most common and important application areas • The problem: • Data is stored in a list of records, each of which includes a distinct identifying key • Given a key (called the target), determine whether the list contains a record with key=target and, if so, return its location in the list

  3. Searching • Very time consuming • a common operation • lists may be very large • even small improvements pay off • Not all searches are created equal... • Searching in unordered lists (sequential search) • Searching in ordered lists (binary search) • searching for patterns in text (various algorithms) • Internal searching vs. External searching

  4. Sequential Search • If list is not ordered or organized in any particular way, this is the only method: • Algorithm: Compare target to the key of each item in list until either: • target is found (return location) • the list is exhausted (signal “not found”) • O(n) • Java code

  5. Binary Search - sketch • For ordered lists only Compare target to the key of item in the middle of the list Three cases: • Equal: target is found (return location) • Less: search first sublist • Greater: Search second sublist

  6. Binary Search - Recursive algorithm If list has at least one item: {Compare target to the key of item in the middle of the list Three cases: • Equal: target is found (return location) • Less: search first sublist • Greater: Search second sublist } else signal “not found”

  7. Binary search • Much more efficient than sequential search • O(lg n) • Java code for recursive version

  8. Binary Search - iterative version • Eliminate recursion by keeping track of current search interval in two variables (low and high) • Algorithm (given list a[0]..a[n-1]): • low = 0 • high = n • while low <high (we still have at least one item) • mid = (low+high)/2 • compare target to the key of a[mid]. Three cases: • Equal: return location (target is found) • Less: high = mid (search first sublist) • Greater: low = mid+1 (Search second sublist)

  9. Binary search - iterative version • Still same efficiency class: O(lg n) • Java code for iterative version • Java code for polymorphic version

  10. Sequential Search Slow - O(n) Linked or array implementation Applicable to any list Easier to implement low overhead Binary Search Fast - O(lg n) array implementation only list must be ordered Comparison of Searching methods

More Related