Tutorial 09 - //found it if (x < arr[mid]) return

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

View Full Document Right Arrow Icon
p q r s t COMP2781, COMP8781, Sem. 1, 2011, Flinders University Tutorial 9 Q1. It takes 18 seconds to complete a task of size 6 for algorithm A when run on computer C. Al- gorithm A is of order n 3 . How much time would it take to complete three times bigger task on the computer C? How long would it take to complete the task on a computer ten times slower than computer C? Q2. Analyse time complexity of the binary search algorithm (implemented in Java). int binsearch(int arr[], int x, int left, int right){ if (left<=right) { int mid=(left+right)/2; if (x==arr[mid]) return mid;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //found it if (x &lt; arr[mid]) return binsearch(arr,x,left,mid-1); else return binsearch(arr,x,mid+1,right); } return -1; // Failed to find x } Note how the recursive version checks to see if we are at x, then it returns, otherwise it calls itself to solve a smaller problem (see also Epp, Sec. 11.5). Observe that, if a n denotes the worst-case run time, then a n = a n 2 + 1 , and a n O ( log n ) . Q3. Sec. 10.1, Ex. 19, 20, 33, 43 Q4. Sec. 10.2, Ex. 8, 12, 15, 18 Page 1 Last updated May 18, 2011 by M. Bajger...
View Full Document

Ask a homework question - tutors are online