Professional Documents
Culture Documents
Question 1 Week 3
Question 1 Week 3
Question 1
3.0/3.0 points (ungraded)
1. Which one of these relations on strings is not a total ordering?
Question 2
1.0/2.0 points (ungraded)
1. Assuming you sort an array of size 25 with an insertion sort. How many
comparisons can you perform, assuming that the insertion sort is written
optimally? Check all possible answers.
20
50 correct
100 correct
200 correct
300 correct
500
1000
incorrect
Let me explain:
For a size N, you cannot do fewer than N-1 comparisons. The maximum
number of comparisons for the insertion sort is N (N-1) / 2, which is 300 for
N=25.
2. Consider an array of integers, which has a size of 2N, where the element at
index X (0 ≤ X < 2N) is X xor 3 (that is, two least significant bits of X are
flipped). What is the running time of the insertion sort on this array?
Θ(2N) correct
Θ(N2N)
Θ(N22N)
Let me explain:
The only inversions in this array are limited to groups of elements at indices
4T, 4T+1, 4T+2, 4T+3 for all integer T. The number of inversions for each
such group is at most constant (six, to be precise), and the number of groups
is linear in the size of the array, so the overall running time of the insertion
sort is linear as well.
Question 3
Bookmark this page
Question 3
2.0/2.0 points (ungraded)
1. Which of the following is true?
To solve the scalar maximization problem, one shall use only sorting
algorithms which compare and swap adjacent elements, such as the insertion
sort.
To solve the scalar maximization problem, one shall use only sorting
algorithms which are designed specifically to sort integers.
To solve the scalar maximization problem, one may use any possible
sorting algorithm. correct
Let me explain:
Although our proof of optimality of sorted sequences as a solution to the
scalar maximization problem is based on comparing and swapping only
adjacent elements, the results of all sorting algorithms are indistinguishable
from the point of view of this problem. So, all sorting algorithms can be used.
2. Which of the following is true?
Question 4
0.0/1.0 point (ungraded)
In which of the following cases the array is correctly split into two parts with respect
to the given pivot element?
[1, 10, 25, 100, 25, 154], pivot = the first 25 correct
Let me explain:
In the correct cases any element to the left of the pivot is less or equal to the pivot,
and any element to the right of the pivot is greater or equal to the pivot
Question 5
Bookmark this page
Question 5
0.0/1.0 point (ungraded)
Which of the following strategies of choosing the pivot may make Quicksort always
running in Θ(N2) time on some arrays? (The variables s and e are the boundaries of
the part of the array which is being sorted.)
s correct
e correct
random pivot
(s+e)/2 correct
incorrect
Let me explain:
Choosing either s or e leads to quadratic runtime on sorted arrays. There also exists
a bad case for the (s+e)/2 pivot (see Lecture 4). On the contrast to these, choosing a
random pivot leads to expected O(N log N) runtime on any input. or N=25.
Question 6
Bookmark this page
Question 6
1.0/2.0 points (ungraded)
1. Assume that the subarrays L=[2, 10, 25] and R=[1, 14, 18] are merged during
Mergesort. Choose the resulting array.
The procedure correctly merges any subarrays into a sorted array in O(N)
The procedure correctly merges sorted subarrays into a sorted array in
O(N) correct
The procedure correctly merges any subarrays into a sorted array in ϴ(N
log N)
The procedure correctly merges sorted subarrays into a sorted array in
ϴ(N log N) incorrect
Let me explain:
To get a correctly sorted array using the merge procedure, the input
subarrays should be already sorted. The merge procedure writes every
element to the scratch array exactly once, so its runtime is O(N).