Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic.

Similar presentations


Presentation on theme: "1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic."— Presentation transcript:

1 1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic

2 2 Searching b One of the most common and important application areas b The problem: Data is stored in a list of records, each of which includes a distinct identifying keyData 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 listGiven 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 3 Searching b Very time consuming a common operationa common operation lists may be very largelists may be very large even small improvements pay offeven small improvements pay off b Not all searches are created equal... Searching in unordered lists (sequential search)Searching in unordered lists (sequential search) Searching in ordered lists (binary search)Searching in ordered lists (binary search) searching for patterns in text (various algorithms)searching for patterns in text (various algorithms) Internal searching vs. External searchingInternal searching vs. External searching

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

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

6 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)Equal: target is found (return location) Less: search first sublistLess: search first sublist Greater: Search second sublistGreater: Search second sublist} else signal “not found”

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

8 8 Binary Search - iterative version  Eliminate recursion by keeping track of current search interval in two variables ( low and high ) b Algorithm (given list a[0]..a[n-1]): low = 0low = 0 high = nhigh = n while low <high (we still have at least one item)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 9 Binary search - iterative version b Still same efficiency class: O(lg n) b Java code for iterative version Java code for iterative version Java code for iterative version b Java code for polymorphic version Java code for polymorphic version Java code for polymorphic version

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


Download ppt "1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic."

Similar presentations


Ads by Google