Data Str & Algorithm HW Solutions 58

Data Str & Algorithm HW Solutions 58 - while (r != l+1)...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
9 Searching 9.1 The graph shows a straight line from ( n +1) / 2 when p 0 =0 to n when p 0 = 1 . Thus, the growth rate in the cost for a search is linear as the probability of an unsuccessful search increases. 9.2 int dictsrch(int array[], int K, int left, int right, int low, int high) { // left and right are array bounds. low and high are // key range bounds. Return position of the element // in array (if any) with value K int i; double fract; int l = left-1; int r = right+1; // l and r beyond bounds of array if (K == high) // Special case if (K == array[right}) return right; else return UNSUCCESSFUL;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while (r != l+1) { // Stop when l and r meet // Compute where in the current range K will be fract = (double)(K - low)/(double)(high - low); // Set pos to check at that fraction of bounds i = l+1 + (int)(fract * (double)(r - l - 1)); // i will be between l and r, non-inclusive, // so progress must be made // Now, check that position and update ranges if (K < array[i]) { r = i; high = array[i]; } if (K == array[i]) return i; if (K > array[i]) { l = i; low = array[i]; } } return UNSUCCESSFUL; // key value not found 58...
View Full Document

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online