Practice Midterm Flashcards

1
Q

We consider quicksort with the auxiliary procedure for partition. Suppose that partition splits the array always in a 11-to-1 proportional split, so one part has length 1/12 and the other part has length 11/12. Then we have the following:

A) The recurrence for the running time of quicksort is T(n)= T(n/12) + T(11n/12) + cn with c a constant, and quicksort runs hence in O(n log n) time.

B) The recurrence for the running time of quicksort is T(n) = 2T(n/2) +cn with c a constant, and quicksort runs hence in O(n log n) time.

C) The recurrence for the running time of quicksort is T(n) = 12T(n/12) + cn with c a constant, and quicksort runs hence in O(n log n) time.

D) The recurrence for the running time of quicksort is T(n) = T(n/12) + T(11n/12) + cn with c a constant, and quicksort runs hence in O(n2) time.

A

A) The recurrence for the running time of quicksort is T(n)= T(n/12) + T(11n/12) + cn with c a constant, and quicksort runs hence in O(n log n) time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

We perform the procedure partition which is used in quicksort. The resulting array is [2, 3, 1, 4, 5, 8, 7, 9, 10]. Which are all keys that could have been the pivot?

A) 4, 5, 9, 10

B) 5, 9, 10

C) 4, 5, 8, 10

D) 4, 5, 8, 9, 10

A

A) 4, 5, 9, 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Consider the pseudocode for partition. What is a drawback?

A) We have to traverse the array completely.

B) If the array is sorted, we swap every element with itself.

C) It contains a for-loop.

D) We use two indices.

A

B) If the array is sorted, we swap every element with itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following statements are true?

1) Heapsort is a divide-and-conquer algorithm.

2) Insertion sort can be faster than merge sort.

3) Heapsort is asymptotically optimal.

4) Bucket sort needs additional memory.

A) Only 2, 3, 4 are true.

B) Only 1, 2 are true.

C) Only 2, 3 are true.

D) Only 2, 4 are true.

A

A) Only 2, 3, 4 are true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following statements are true?

1) Swapping two elements of an array can be done in constant time.

2) The smallest key of a max-heap is at the leftmost leaf.

3) All algorithms for turning an array into a max-heap are in Omega (n log(n)).

4) For all comparison-based sorting algorithms the worst-case time complexity is in Omega (n log(n)).

A) Only 1, 4 are true.

B) Only 1 is true.

C) Only 2 and 3 are true.

D) Only 1, 3, 4 are true.

E) Only 2, 3, 4 are true.

A

A) Only 1, 4 are true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

We perform the bottom-up procedure for building a max-heap. What is the result of performing (only) the first iteration of the for-loop on input A = [8, 1, 6, 4, 3, 7, 9]?

A) [8,1,9,4,3,7,6]

B) [8,4,9,1,3,7,6]

C) [9,1,6,4,3,7,8]

D) [9,1,8,4,3,7,6]

A

A) [8,1,9,4,3,7,6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the result of adding the key 8 to the max-heap [9, 7, 4, 6, 5, 1, 3, 0, 0, 0 ] with heapsize 7?

A) [9, 7, 8, 6, 5, 1, 3, 4, 0, 0]

B) [8, 7, 4, 6, 5, 1, 9, 0, 0, 0]

C) [8, 7, 4, 6, 5, 1, 3, 9, 0, 0]

D) [9, 8, 4, 7, 5, 1, 3, 6, 0, 0]

A

D) [9, 8, 4, 7, 5, 1, 3, 6, 0, 0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean that the lower-bound on the worst-case time complexity for comparison-based sorting is Omega (n log(n))?

A) It means that using comparison-based sorting we cannot sort faster than in n log(n).

B) It means that the worst-case of a comparison-based sorting algorithm is at least n log(n).

C) It means that the worst-case of a comparison-based sorting algorithm is at most n log(n).

D) It means that using comparison-based sorting we cannot sort slower than in n log(n).

A

B) It means that the worst-case of a comparison-based sorting algorithm is at least n log(n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following recurrence equations with T(0) = 1 gives T(n) in Theta(n^2)?

A) T(n) = 2T(n/2) + n

B) T(n) = T(n-1) + 1

C) T(n) = T(n-1) + n

D) T(n) = T(n-1)

A

C) T(n) = T(n-1) + n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following is(are) in increasing rate of growth?

A) 2^7, n log(n), n^2, n!

B) 3n, 2^3, n log(n), n^3

C) n^2, n^3, n log(n), 2^n

D) n log(n), n^2, n^7, 2^n

E) n log(n), n^2, 2^7, n!

A

A) 2^7, n log(n), n^2, n!

D) n log(n), n^2, n^7, 2^n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a reason to use insertion sort as subroutine in bucket sort?

A) It has a good worst-case time complexity.

B) It has a good best-case time complexity.

C) It performs relatively well on small inputs.

D) It performs relatively well on lists.

A

C) It performs relatively well on small inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following arrays is a max-heap?

A) [1,2,3,4,5,6,7]

B) [7,3,1,2,6,4,5]

C) [7,3,6,1,2,5,4]

D) [7,6,4,5,3,1,2]

E) [7,6,5,4,3,2,1]

F) [7,6,4,3,1,5,2]

A

C) [7,3,6,1,2,5,4]

D) [7,6,4,5,3,1,2]

E) [7,6,5,4,3,2,1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the advantage of implementing a min-priority queue using a heap?

A) Removal is in constant time.

B) Adding is in constant time.

C) Adding and deleting are equally expensive.

D) Adding and deleting are both faster than if we use an array.

A

C) Adding and deleting are equally expensive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

We apply heapsort to an array where all n elements are the same. What is its performance in this case?

A) n log(n) because in the for-loop we execute n times MaxHeapify

B) n log(n) because half of the nodes are already max-heaps

C) n because in the for-loop we perform n times elementary time work

D) n because we can build a max-heap in linear time

A

D) n because we can build a max-heap in linear time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the best-case time complexity of selection sort?

A) O(1) because we find immediately the smallest elements of the unordered part.

B) O(n) because we just check whether the array is ordered.

C) O(n log(n)) because that is the best-case for comparison-based sorting.

D) O(n^2) because we always have to completely traverse the unordered part.

A

D) O(n^2) because we always have to completely traverse the unordered part.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

We apply heapsort to an array which is already ordered. What is the performance in this case?

A) O(n log(n)) because then BuildMaxHeap is in n log(n)

B) O(n) because we just verify that the input is ordered

C) O(n) because MaxHeapify then performs in constant time

D) O(n log(n)) because MaxHeapify performs in log(n)

A

D) O(n log(n)) because MaxHeapify performs in log(n)

17
Q

We perform heapsort. Which of the following can be an intermediate result after performing some but not all iterations?

A) [7,6,4,5,3,2,1,8,9,10]

B) [7,3,4,2,1,6,5,8,9,10]

C) [8,7,6,5,4,3,2,1,10,9]

D) [8,6,5,3,4,1,2,7,9,10]

A

A) [7,6,4,5,3,2,1,8,9,10]

18
Q

We consider two statements.

Statement A is: 2^(n+1) is in O(2^n).

Statement B is: 2^(2n) is in O(2^n). Which of the following holds?

A and B are both false

A and B are both true

A is true but B is false

B is true but A is false

A

A is true but B is false

19
Q

We consider bucket sort. What is the worst-case running time of bucket sort ?

A) It is in Theta (n^2), because we may call insertion sort, which is quadratic, on an input of size n, namely if (floor of nA[i]) is the same for all indices i.

B) It is in Theta (n^3), because we may call insertion sort which is quadratic n times.

C) It is in Theta (n), because the for-loops are not nested.

D) It is in Theta (n^2), because worst-case sorting is always in n^2.

A

A) It is in Theta (n^2), because we may call insertion sort, which is quadratic, on an input of size n, namely if (floor of nA[i]) is the same for all indices i.

20
Q

We consider a decision tree for a comparison-based sorting tree for sorting an array of size n. The smallest possible depth of a leaf is

A) log(n)

B) n

C) n!

D) n log(n)

A

B) n

21
Q

We apply ExtractMax to a max-heap; the result is output 10 and the max-heap [6,3,5,2,1]. Which of the following could have been the input?

A) [10,6,5,3,2,1]

B) [10,3,6,2,1,5]

C) [10,6,5,2,3,1]

D) [10,5,6,2,1,3]

E) [10,5,1,2,3,6]

A

B) [10,3,6,2,1,5]

C) [10,6,5,2,3,1]

22
Q

We have T(n) in O(n^2) with T a function for the worst-case time complexity of algorithm A. Do we have T(n) in Theta(n^2)?

Yes, because an upper bound for the worst-case gives a lower bound.

No, T is not necessarily bound from below by n^2.

A

No, T is not necessarily bound from below by n^2.

23
Q

We perform quicksort on an input-array A consisting of n zeroes. What happens?

A) We get the expected n log(n) performance.

B) We get a quadratic worst-case performance.

C) We get the best-case n log(n) performance.

D) We get the best-case linear performance.

A

B) We get a quadratic worst-case performance.

24
Q

We perform the bottom-up heap construction on the array [1,2,3,4,5,6,7]. What is the result?

A) [7,6,5,4,3,2,1]

B) [7,5,6,4,2,1,3]

C) [7,6,5,4,2,1,3]

D) [7,5,6,2,4,1,3]

A

B) [7,5,6,4,2,1,3]

25
Q

In the recurrence tree for merge sort

A) the height is log(n) and the work per layer is the linear work to split.

B) the height is log(n) and the work per layer is the linear work to merge.

C) the height is n and the work per layer is the linear work to split.

D) the height is n and the work per layer is the linear work to merge.

A

B) the height is log(n) and the work per layer is the linear work to merge.

26
Q

Consider the pseudocode for partition. How can we optimize it a bit?

A) Start the for-loop with index j on the first element of A that is larger than the pivot, and index i one before.

B) Start the for-loop with indices i and j both on the first element of A that is larger than the pivot.

C) Start the for-loop with indices i and j both on the index before the first element of A that is larger than the pivot.

D) Start the for-loop with index i on the first element of A that is larger than the pivot, and index j one before.

A

A) Start the for-loop with index j on the first element of A that is larger than the pivot, and index i one before.

27
Q

It is possible that an algorithm for MaxHeapify in Theta(1) will be found.

True.

False.

A

False.