## Description

**Problem Set 5**

**Sequential Search, Binary Search**

- Given the following sequence of integers:

3, 9, 2, 15, -5, 18, 7, 5, 8

- What is the average number of comparisons for a successful search assuming all entries are searched with equal probability? Show your work.
- Suppose the search probabilities for the elements of this list are, respectively: 1, 0.3, 0.05, 0.2, 0.05, 0.1, 0.05, 0.1, 0.05

What is the average number of comparisons for successful search with these search probabilities? Show your work.

- Rearrange the list entries in a way that would result in the lowest number of comparisons on the average for successful search, given the above probabilities of search. What is this lowest number of comparisons? Show your work.

- An adaptive algorithm to lower average match time for sequential search is to move an item by one spot toward the front every time it is matched. (Unless it is already at the front, in which case nothing is done on a match.) Complete the following modified sequential search on a linked list with this move-toward-front adaption. Assume a generic Node class with data and next

public class LinkedList<T> {Â Â Â Â Â Â Â Â Â private Node<T> front;Â Â Â Â Â Â Â Â Â int size;

…

// moves the target one place toward the front

// doesn’t do anything if target is in the first node

// returns true if target found, false otherwiseÂ Â Â Â Â Â Â Â Â public boolean moveTowardFront(T target) {

// COMPLETEÂ THIS METHOD

}

}

- Draw the comparison tree for binary search on a sorted array of length 11.
- What is the worst case number of comparisons for success? For failure?
- What is the average number of comparisons for success, assuming equal likelihood of success at any spot?
- What can you say about the average number of comparisons for failure? Can you approximate it within a small range? Does it depend on the distribution of the probabilities of failing at the various failure spots?

*****A variant of binary search, called*lazy*binary search, works as described in the following algorithm, where t is the target to search, and n is the size of the array:

left <– 0Â Â Â Â right <– n-1Â Â Â Â while (left < right) doÂ Â Â Â Â Â Â Â mid <– (left + right)/2Â Â Â Â Â Â Â if (t > A[mid]) thenÂ Â Â Â Â Â Â Â Â Â left <– mid + 1Â Â Â Â Â Â Â elseÂ Â Â Â Â Â Â Â Â Â Â right <– midÂ Â Â Â Â Â Â endifÂ Â Â Â endwhile

if (t == A[left]) then

display “found at position”, leftÂ Â Â Â else

display “not found”Â Â Â Â endif

- Trace this algorithm on the following array, with 46 as the search target:

10Â Â 15Â Â 25Â Â 30Â Â 45Â Â 46Â Â 48Â Â 72Â Â 76Â Â 80Â Â 93

How many comparisons are made by the time a match is found? How does your answer compare with that for regular binary search?

- Repeat with 40 as the target. How many comparisons are made until failure is detected? How does your answer compare with that for regular binary search?
- Draw the comparison tree for lazy binary search on an array of length 11 (same length as the example array above).
- What is the worst case number of comparisons for success? For failure?
- What can you say about the range of values for the average number of comparisons for success? For failure?
- Under what conditions is it preferable to use lazy binary search over the regular binary search?
- An alternative algorithm for searching on a sorted array of size
*n*works as follows. It divides the array into*m*contiguous blocks each of size*s*. (Assume that*s*divides into*n*without remainder).

Here is the algorithm to search for a key *k* in sorted array *A*.

Compare *k* with the last entry in the first block, i.e. *A[s-1]*

If there is match, then stop with success

Otherwise, check if *k* < *A[s-1]*

If so, perform a sequential search on the block of entries fromÂ *A[0]* to *A[s-2]*. If there is a match, stop with success, otherwise stop with failure.

If *k* is not < *A[s-1]* then continue the process by repeating the above on the second block, and so on.

- What is the
**worst**case number of searches for success? ******What is the**average**case number of searches for success?