• Nie Znaleziono Wyników

CIS435, Midterm, March 11, 2003.

N/A
N/A
Protected

Academic year: 2021

Share "CIS435, Midterm, March 11, 2003."

Copied!
1
0
0

Pełen tekst

(1)

CIS435, Midterm, March 11, 2003.

W. Rytter

Problem 1 (Recurrences) (7 points) Let T (1) = 1, assume n is a power of 2, solve the following recurrences, use tight asymptotic notation for T (n) (exact formulas not required), where:

(a) T (n) = T (

n2

) + n;

(b) T (n) = 2T (

n2

) + n;

(c) T (n) = 8T (

n2

) + n.

Problem 2 (Algorithm analysis) (8 points)

What is the value of s returned in each one of the programs below as a function of n? Use asymptotic notation to express this value in terms of n. Provide also exact closed formulas. Write short justification of your answers.

int A(int n) { int B (int n) {

int i,k,s; s=1;k=2; int i,s; s=2;k=1;

for(i=1;i<=n;i++) for(i=1;i<= n;i++ )

{k=3*k; s=s+k+1;} {s=s+k+2; k=k+1;}

return(s) return(s);

} }

Problem 3 (Max-Heaps) (7 points) Assume we have the array A = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11, 12, 13].

How will this array look after performing Build-Max-Heap(A) using the description of the function Build-Max-Heap(A) from the textbook.

Problem 4 (Quicksort) (7 points) Assume we have the array

A = [1, 2, 10, 3, 11, 4, 12, 13, 5, 6, 7, 14, 15, 7, 8]

How will this array look after performing PARTITION(A, 1, 15), where PARTITION is the function related to Quicksort. Assume the first position of A is 1.

Problem 5 (Algorithm design) (5 points) Assume we are given two sorted arrays A[1..n] and B[1..n] of integers and an integer x.

(a) Describe informally a linear time algorithm which returns 1 if x = a + b, for some a ∈ A, b ∈ B. If there are no such a, b then the value 0 is returned.

(b) Write the algorithm as a function T EST (A, B, x) in the pseudo-code.

Hint: Scan the array A left-to-right and B right-to-left in a synchronized way.

Cytaty

Powiązane dokumenty

(b) Calculate the probability that after five days the price will rise by $1, if we know that after two days the price level was equal to the initial level?. (c) Are the

[r]

Lady Vernon was gone, he looked over his note-book for a time, and raising his eyes a little after, he saw the slight figure of Miss Maximilla Medwyn walking up and down

Although we have, for this general case, some asymp- totic distributions (see for example Box (1949) and Anderson (2003)) and some near-exact distributions (Coelho, 2003, 2004), in

Krajowy Zjazd Delegatów popiera i soli- daryzuje się z prowadzoną przez Sekcję Krajową Kolejarzy akcją protestacyjną, do strajku generalnego włącznie, prowadzoną

Wyłania się powoli układ poparcia, który to utrzymywał jego pozycję przez poprzednią kadencję a i walnie przyczynił się do jego ponow­.. nego wyboru, choć

The Court of Justice of the European Union shall have jurisdiction in actions on grounds of infringement of the principle of subsidiarity by a legislative act, brought in

Write the related algorithm in the pseudocode (informal programming language used in the