Docsity

4 Practice Questions on Algorithm and Systems Analysis - Midterm Exam | CSE 21, Exams of Computer Science

Toggle sidebar
bg1
CSE 21
Practice Exam for Midterm 1
Spring, 2015
1. Sorting and searching Give the number of comparisons that will be performed by each sorting algo-
rithm if the input array of length nhappens to be of the form [1,2, ..., n −3, n −2, n, n −1] (i.e., sorted
except for the last two elements).
(a) MinSort (SelectionSort)
(b) BubbleSort
(c) RevisedBubbleSort
(d) InsertionSort
2. Order questions For each part, answer True or False, and give a short explanation for your answer. All
logarithms are base 2.
(a) √n3∈O(n2).
(b) 8log(n2)∈Θ(n6).
(c) log(n)∈Ω(log(log(n))).
(d) If f,g, and hare functions from the natural numbers to the non-negative real numbers with
f(n)≥g(n)∀n≥1, f(n)∈Θ(h(n)), and g(n)∈Θ(h(n)), then (f−g)(n)) ∈Θ(h(n)).
(e) If f,g, and hare functions from the natural numbers to the non-negative real numbers with
f(n)∈Θ(h(n)) and g(n)∈Θ(h(n)), then (f∗g)(n)) ∈Θ((h(n))2).
3. Analyzing an iterative algorithm In the following problem, we are given an array of salaries of em-
ployees at our firm and two numbers Land H. We wish to compute the average salary of employees
who earn between Land H(inclusive), and the number of such employees. If there are no employees
in the range, we say that 0 is the average salary. This is an iterative algorithm which takes as input
A[1, . . . , n], L, and Hand returns an ordered pair (a, N ) where ais the average salary of employees in
the range, and Nis the number of employees in the range.
AverageI nRange(A[1, . . . , n], L, H).
1. Sum ←0
2. N←0
3. FOR I= 1 TO nDO:
4. IF L≤A[I]≤HTHEN Sum ←Sum +A[I], N + +
5. IF N= 0 THEN Return (0,0)
6. Return (Sum/N, N )
(a) State a loop invariant that can be used to show the algorithm AverageInRang e is correct.
(b) Prove your loop invariant from part (a).
(c) Conclude from the loop invariant that the algorithm AverageInRang e is correct.
(d) Describe the running time of this algorithm in Big O notation, assuming that comparisons and
arithmetic operations take constant time. Justify your answer.
1
pf2

Partial preview of the text

Download 4 Practice Questions on Algorithm and Systems Analysis - Midterm Exam | CSE 21 and more Exams Computer Science in PDF only on Docsity!

CSE 21

Practice Exam for Midterm 1

Spring, 2015

  1. Sorting and searching Give the number of comparisons that will be performed by each sorting algo- rithm if the input array of length n happens to be of the form [1, 2 , ..., n − 3 , n − 2 , n, n − 1] (i.e., sorted except for the last two elements). (a) MinSort (SelectionSort) (b) BubbleSort (c) RevisedBubbleSort (d) InsertionSort
  2. Order questions For each part, answer True or False, and give a short explanation for your answer. All logarithms are base 2. (a)

n^3 ∈ O(n^2 ). (b) 8log(n^2 )^ ∈ Θ(n^6 ). (c) log(n) ∈ Ω(log(log(n))). (d) If f , g, and h are functions from the natural numbers to the non-negative real numbers with f (n) ≥ g(n)∀n ≥ 1, f (n) ∈ Θ(h(n)), and g(n) ∈ Θ(h(n)), then (f − g)(n)) ∈ Θ(h(n)). (e) If f , g, and h are functions from the natural numbers to the non-negative real numbers with f (n) ∈ Θ(h(n)) and g(n) ∈ Θ(h(n)), then (f ∗ g)(n)) ∈ Θ((h(n))^2 ).

  1. Analyzing an iterative algorithm In the following problem, we are given an array of salaries of em- ployees at our firm and two numbers L and H. We wish to compute the average salary of employees who earn between L and H (inclusive), and the number of such employees. If there are no employees in the range, we say that 0 is the average salary. This is an iterative algorithm which takes as input A[1,... , n], L, and H and returns an ordered pair (a, N ) where a is the average salary of employees in the range, and N is the number of employees in the range. AverageInRange(A[1,... , n], L, H). 1. Sum ← 0 2. N ← 0 3. FOR I = 1 TO n DO: 4. IF L ≤ A[I] ≤ H THEN Sum ← Sum + A[I], N + + 5. IF N = 0 THEN Return (0, 0) 6. Return (Sum/N, N ) (a) State a loop invariant that can be used to show the algorithm AverageInRange is correct. (b) Prove your loop invariant from part (a). (c) Conclude from the loop invariant that the algorithm AverageInRange is correct. (d) Describe the running time of this algorithm in Big O notation, assuming that comparisons and arithmetic operations take constant time. Justify your answer.
  1. Recursion In the following problem, we are given an array of salaries of employees at our firm and two numbers L and H. We wish to compute the average salary of employees who earn between L and H (inclusive), and the number of such employees. If there are no employees in the range, we say that 0 is the average salary. This is a recursive algorithm which takes as input A[1,... , n], L, and H and returns an ordered pair (a, N ) where a is the average salary of employees in the range, and N is the number of employees in the range. RecAIR(A[1,... , n], L, H). 1. IF n = 0 THEN Return (0, 0) 2. (a, N ) ← RecAIR(A[1,... , n − 1], L, H) 3. IF L ≤ A[n] ≤ H THEN Return ((a ∗ N + A[n])/(N + 1), N + 1) 4. ELSE Return (a, N ) (a) Prove by induction on n that for any input, the algorithm correctly returns the average salary and number of employees in the range. (b) Write down a recurrence for the time taken by this algorithm, assuming that comparisons and arithmetic operations take constant time. (c) Use your answer from part (b) to determine the running time of this algorithm in Big O notation.