Data Str & Algorithm HW Solutions 20

Data Str & Algorithm HW Solutions 20 - 20 Chap. 3...

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 20 Chap. 3 Algorithm Analysis 3.15 Yes. When we specify an upper or lower bound, that merely states our knowl- edge of the situation. If they do not meet, that merely means that we dont KNOW more about the problem. When we understand the problem com- pletely, the bounds will meet. But, that does NOT mean that we can actually determine the optimal algorithm, or the true lower bound, for every problem. 3.16 // Return position of first elem (if any) with value K int newbin(int K, int* array, int left, int right) { int l = left-1; int r = right+1; // l and r beyond array bounds while (l+1 != r) { // Stop when l and r meet int i = (l+r)/2; // Look at middle of subarray if (K <= array[i]) r = i; // In left half if (K > array[i]) l = i; // In right half } if (r > right) return ERROR; // K not in array if (array[r] != K) return ERROR; // K not in array else return r; // r at value K } 3.17 int newbin(int K, int* array, int left, int right) { // Return position of greatest element <= K int l = left-1;...
View Full Document

Ask a homework question - tutors are online