Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms

2 2 Binary Search—Forgetful version Examines the element in the middle of the array. Is the middle element too small? Then start looking in second half of array. Otherwise: Begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when there is only one item left, or when there is nowhere else to search. If the one item matches the target, the search was successful. Otherwise the item was not in the list.

3 3 Binary Search-- The forgetful version Error_code recursive_binary_1(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom < top) { // List has more than one entry. //we will work this out in class } else if (top < bottom) { return not_present; // List is empty. } else { // List has exactly one entry. position = bottom; the_list.retrieve(bottom, data); if (data == target) return success; else return not_present; }

4 4 Binary Search-- The forgetful version Error_code recursive_binary_1(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom < top) { // List has more than one entry. int mid = (bottom + top)/2; the_list.retrieve(mid, data); if (data < target) // Reduce to top half of list. return recursive_binary_1(the_list, target, mid + 1, top, position); else // Reduce to bottom half of list. return recursive_binary_1(the_list, target, bottom, mid, position); } else if (top < bottom) { return not_present; // List is empty. } else { // List has exactly one entry. position = bottom; the_list.retrieve(bottom, data); if (data == target) return success; else return not_present; }

5 5 Binary Search—recognizing equality Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when item is found, or when there is nowhere else to look and it has not been located.

6 6 Binary Search-- Recognizing equality Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom <= top) { // List has more than one entry. //we will work this out in class } else { return not_present; // List is empty. }

7 7 Binary Search-- Recognizing equality Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if (bottom <= top) { // List has more than one entry. int mid = (bottom + top)/2; the_list.retrieve(mid, data); if (data == target) { position = mid; return success; } else if (data < target) // Reduce to top half of list. return recursive_binary_2(the_list, target, mid + 1, top, position); else // Reduce to bottom half of list. return recursive_binary_2(the_list, target, bottom, mid -1, position); } else { return not_present; // List is empty. }

8 8 Comparison Trees Tree for sequential search: root vertex (node) height level 0 level 1 level 2 parent child leaf

9 9 Comparison tree for binary search 1

10 10 Average number of comparisons for binary search 1 The number of comparisons = external path length External path length = Sum of the number of branches traversed in going from root to leaf once for each leaf. For n = 10, external path length = ? Answer: ((4*5) + (6*4))*2 = 88 Half the leaves are for successful searches. Half the leaves are for unsuccessful searches. Average number of comparisons for successful searches is? Answer: 0.5(88)/10 = 4.4 Average number of comparisons for unsuccessful searches is? Answer: 4.4

11 11 Comparison tree for binary search 2

12 12 Average number of comparisons for binary search 2 For successful searches--average number of comparisons is related to the internal path length. Internal path length = sum of the number of branches traversed from root to vertex for each non-leaf vertex. Internal path length for n = 10 is ? Answer: (0+1+2+2+3+1+2+3+2+3) = 19 Number of vertices traversed on each path is one more than path length. Number of comparisons is 2 for each non-terminating vertex and 1 for each terminating vertex. Total comparisons = ? Answer: 2*(19 + 10) - 10 = 48 Average number of comparisons = ? Answer: 48/10 = 4.8 (worse than binary search 1)

13 13 Average number of comparisons for binary search 2 For unsuccessful searches--average number of comparisons is related to external path length. For binary search 2 tree, external path length = ? Answer: (5*3) + (6*4) = 39 Since there are 2 comparisons per vertex, and 11 possible unsuccessful terminations, then: Average number of comparisons = ? Answer: 2*39/11 = 7.1

14 14 In general, for lists of length n: SuccessfulUnsuccessful Binary Search 1 lg n + 1 lg n + 1 Binary Search 2 2 lg n - 3 2 lg n

15 15 Graphing the comparisons


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms."

Similar presentations


Ads by Google