# ADA - ADA algo solution maual 1 Any algo whose worst...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: ADA algo solution maual 1 Any algo whose worst running time is lowest, is least dependent on the initial order of input (or in general, is least dependent on anything at all - since we've assuming the worst that could ever happen anyways ) So amongst comparison sorts, the running time of a merge sort or a heap sort would be least dependent (or as I said, not at all dependent) on your initial input ordering . 2 It needs O(n) time and O(1) space. Just a minor modification of the basic algorithm to determine max element in an array. .... max = A[0] .... count = 1 .... for(i = 1; i < N; i++) ..... begin ...... if(A[ i ] == max) ........... ++count ...... else if(A[ i ] > max) ........... max = A[ i ] ........... count = 1 //mark this step - found a new max element hence reset count ...... endif .... endfor .... print "Max value = " max .... print "Number of occurences = " count Time = O(n) Space = O(1) 3. time complexity of code for(int i=0;i<=n;i++) for(j=0;j<=log(i);j++) printf("puneet") printf executes for log(1) + log(2) + log(3) + log(4) + log(5) + ...log(n) times Page 1 ADA = log (n!) times so complexity is O(log(n!)) think u'll have to take into account the complexity of the function log(i) here coz its calculated from the series ln(1+x)=x-(x^2/2)+(x^3/3)-(x^4/4)+... now obviously this is not linear time, and it depends on the accuracy till which the maths library calculates it. Sot i dont think he complexity is O(log(n!)). printf executes for (log(1) +1)+ (log(2)+1) + (log(3)+1) + (log(4)+1) + ...(log(n)+1) times = (n+log (n!)) times still the complexity is O(log(n!)) becoz log(n!) is greater than n for larger values of n.eg if base of log is "e" then for n>=6 log(n!) is greater than n. if base of log is 10 then for n>=25 log(n!) is greater than 4.for(i=0;i<=n*n;i++) for(j=i;j<=n/2;j++) printf("x"); I think its O(n^3)...And moreover there isn't any hidden landmine in it as well break outer loop into 2 parts 1) 0<i<=(n/2) (usual nested loop) 2) n/2<i<=n*n (inner loop doesn't work) so both r O(n^2) 5ques:> the input is a set s containing n real number sorted in increasing order and a real number x we have to develop algo to know wheather there are two elements of s whose sum is exactly x use linear search steps: 1. move in the array and stop at the point where u get an element >=x 2. now subtract that number from x and search the number in the array to the left from start.. 3. now move backwards from the number(where we stopped) and repeat the same for all other Page 2 ADA let us say the array is 1,2,5,6,7,8,19,20,34,36,45,76,86 we have to search for the sum X=38 1. start from 1....stop at 36 2. now search for the number 38-36=2 in the left side of the array 3. suppose if 2 was not there....then move backwards from 36 to 34 and repeat step 2 or have two pointers one at the beginning and one at the end........
View Full Document