Midterm-Spring04-Solutions - University of Waterloo...

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

View Full Document Right Arrow Icon
SE240 Midterm, Spring 2004 Page 1 of 13 University of Waterloo Department of Electrical and Computer Engineering SE 240 Algorithms and Data Structures Spring 2004 Midterm Examination Instructor: Ladan Tahvildari Date: Monday, June 21, 2004, 7:00 p.m. Duration: 2 hours Type: Closed Book Instructions: There are 5 questions. Answer all 5 questions. The number in brackets denotes the relative weight of the question (out of 100). If information appears to be missing from a question, make a reasonable assumption, state it and proceed. Write your answers directly on the sheets. If the space to answer a question is not sufficient, use the last (overflow) page. When presenting programs, you may use any mixture of pseudocode/C++/Java constructs as long as the meaning is clear. Name Student ID Solution Question Mark Max Marker 1 25 2 15 3 20 4 20 5 20 Total 100
Background image of page 1

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

View Full DocumentRight Arrow Icon
SE240 Midterm, Spring 2004 Page 2 of 13 Name: Student ID: Question 1: Algorithm Analysis [25] Part A. [10] Consider the function which is shown in the following figure. 6 5 sin ) ( 2 2 + = n n n n f 1) Using the definition of “big oh” show that . ) ( ) ( 2 n O n f = By definition, f(n) = O(n 2 ), if there exists c > 0, n > n o such that for all n > n 0 , f(n) < cn 2 . f(n) = n 2 sin 2 n + 5n – 6 < n 2 + 5n – 6 Consider c = 2: n 2 + 5n – 6 < 2n 2 ==> n 2 – 5n + 6 > 0 (n – 2)(n – 3) > 0 ==> n > 3 Therefore, for all n > 3, f(n) < 2n 2 . c = 2 and n 0 = 3 2) Using the definition of “omega” show that ) ( ) ( n n f = . By definition, f(n) = (n), if there exists c > 0, n > n o such that for all n > n 0 , f(n) > cn. f(n) = n 2 sin 2 n + 5n – 6 > 5n – 6 Consider c = 1: 5n – 6 > n ==> 4n > 6 ==> n > 3/2 Therefore, for all n > 3/2, f(n) > n. c = 1 and n 0 = 3/2
Background image of page 2
SE240 Midterm, Spring 2004 Page 3 of 13 Name: Student ID: Part B. [8] Consider the sorting algorithm given below. This algorithm is designed to sort an array of integers, , where is assumed to be a power of two. ] 1 [ ],. .., 1 [ ], 0 [ n a a a n 1 void Merge (int a[], int pos1, int pos2, int len); 2 3 void MSort (int a[], int n) 4 { 5 for (int s = 1; s <= n/2; s = 2*s) 6 for (int i = 0; i < n; i = i + 2*s) 7 Merge (a, i, i + s, s); 8 } This algorithm makes use of a routine called Merge which merges the two sorted sub-sequences of array a , each of length len , which start at array indices pos1 and pos2 as shown below:
Background image of page 3

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

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

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Page1 / 13

Midterm-Spring04-Solutions - University of Waterloo...

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

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