ExamMid1F07-254

8
University of Windsor - School of Computer Science 60-254: Data Structures and Algorithms Fall 2007 - Midterm 1 - Duration: 1h20mn. Instructor: Dr. Robin Gras Student Information Last name: First name: Student number: Read this first • Write your first name, last name and student number. • Make sure your paper contains: 8 pages (including this one), 7 questions. • The exam is open-book and open-notes. • Marking scheme is out of 100. • Answer to all questions. 1

Transcript of ExamMid1F07-254

University of Windsor - School of Computer Science

University of Windsor - School of Computer Science

60-254: Data Structures and Algorithms

Fall 2007 - Midterm 1 - Duration: 1h20mn.

Instructor: Dr. Robin GrasStudent Information

Last name:

First name:

Student number:

Read this first

Write your first name, last name and student number.

Make sure your paper contains:

8 pages (including this one),

7 questions.

The exam is open-book and open-notes.

Marking scheme is out of 100.

Answer to all questions.Question 1(10 marks)What is the smallest big-oh complexity associated to algorithms for which the running time is given by the following functions:

g

Functions

answers

1. f(n) = n2 2n

2. f(n) = n/(n

3. f(n) = log log n + (log n

4. f(n) = n1/4 + log n2

5. f(n) = 2log n + n + 22n

Question 2. (16 marks)What is the value of Sum after the execution of this algorithm with the value: n = 4? What is the big-Oh complexity of the following algorithm? Explain clearly how you came to the answer. You should use one property we have proven during the courses.Sum = 0;

For (i = 0, i n; i++) {

For (j = 1; j i; j++) {

For (k = 1; k j; k++)

Sum++;

/*for (i = 0; i < n; i++)for (j = 0; j < i; j++)A[j] = 0;

Analysis: This code zeroes out the lower triangular part of a two-dimensional array. Since the inner loop is executed a variable amount of times, we can't just multiply n by i:When i = 0 the inner loop is executed 0 times.When i = 1 the inner loop is executed 1 time.When i = 2 the inner loop is executed 2 times.So the number of times the statement "A[j] = 0" is executed is: 1 + 2 + 3 + ... + nThis sum comes up a lot in algorithm analysis. You should memorize the formula:1 + 2 + 3 + ... + n = n(n+1)/2

= 1/2(n2 + n)

Since in Big-Oh analysis we ignore leading constants (the 1/2 in the equation above) and lower order terms (the n in the equation above), this algorithm runs in O(n2) time. */

Big-Oh notation is O(n^3)..Assume sum is 80.

Question 3. (15 marks)Give the proof by induction that 1 un 2 for any integer n 0 when:

Question 4 (20 marks)Write an algorithm for the insertion sort that do not uses any loop but only recursive function calls. Question 5 (10 marks)Apply the quicksort algorithm given in the course to the following list L of numbers. Give the content of L at each step of the algorithm that is each time there is a change in the ordering of the numbers.

L = 7 4 1 3 8 2Question 6 (15 marks)Write a recursive function which return the number of occurrences of a number n in an array a of length lg. What is the complexity of your algorithm?Question 7 (14 marks)Write a divide and conquer algorithm for searching if a number n in present in an unsorted array a. What is the worse case complexity of your algorithm?

PAGE 4

_1253706922.unknown