solmidtW06 - MIDTERM EXAM Dr Malek Mouhoub CS340 Winter...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Dr Malek Mouhoub CS340 – Winter 2006 March 01 2006 Duration : 75 min - Total : 25pts Exercise 1 : Algorithm Analysis [6pts] 1. Give an analysis of the running time (Big-Oh notation) for each of the following 4 program fragments. Note that the running time corresponds here to the number of times the operation sum++ is executed. sqrt is the function that returns the square root of a given number. (a) sum = 0; for(i=0;i<sqrt(n)/2;i++) sum++; for(j=0 ;j<sqrt(n)/4;j++) sum++; for(k=0;k<8+j;k++) sum++; (b) sum = 0; for(i=0;i<sqrt(n)/2;i++) for(j=i;8+i;j++) for(k=j;k<8+j;k++) sum++; (c) sum = 0; for(i=1;i<2*n;i++) for(j=1;j<i*i;j++) for(k=1;k<j;k++) if (j % i == 1) sum++; (d) sum = 0; for(i=1;i<2*n;i++) for(j=1;j<i*i;j++) for(k=1;k<j;k++) if (j % i) sum++; 2. If it takes 10ms to run program (b) for n=100, how long will it take to run for n=400 ? 3. If it takes 10ms to run program (a) for n=100, how large a problem can be solved in 40ms ? Solution
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

solmidtW06 - MIDTERM EXAM Dr Malek Mouhoub CS340 Winter...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online