Question 1: Correct Incorrect

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question 1

 Bookmark this page

Question 1
0.0/1.0 point (ungraded)
Select all the sorting algorithms which are stable and asymptotically optimal.

Quicksort
Insertion sort
Mergesort correct
incorrect
Let me explain:
Quicksort is asymptotically optimal on average, but it is not stable. Insertion sort is
stable, but it is not optimal, as it has O(N2) running time. In this list, only Mergesort is
both stable and asymptotically optimal.

Question 2
 Bookmark this page

Question 2
1.0/1.0 point (ungraded)
Consider the Radix sort algorithm which uses Bucket sort inside to sort by certain
digits. The following array is being sorted: [135, 187, 236, 105, 157, 206]. Select the
intermediate state of this array after the first stage of Radix sort, i.e. after running
Bucket sort for the first time.

[135, 187, 105, 157, 236, 206]


[105, 135, 206, 236, 157, 187]
[135, 105, 236, 206, 187, 157] correct
[105, 135, 157, 187, 206, 236]
Let me explain:
In the first stage of Radix sort, the numbers are compared and sorted by the last digit
using a stable sort (Bucket sort in this case). The numbers are sorted by the last digit
in the second and the third choices. However, in the second choice the mutual
ordering of the numbers with the same last digits (135 and 105, 236 and 206, 187
and 157) is not preserved, so it can not be the result of a stable sort. Therefore, the
only correct answer is the third choice.

Question 3
 Bookmark this page

Question 3
1.0/1.0 point (ungraded)
Which of the listed sorts from the C/C++ standard libraries is generally the fastest
one?

std::stable_sort
std::sort correct
qsort
Let me explain:
In qsort, the comparator is never inlined, so it is slower than C++ sorts. So the right
answer is std::sort, which is generally faster than std::stable_sort

Question 4
 Bookmark this page

Question 4
1.0/1.0 point (ungraded)
Assume that we search where q = 6 is in a sorted array using binary search. Which
sequence of elements x which are compared to q may appear during the search?

9, 10, 4, 5, 6
11, 5, 3, 7, 6
10, 2, 7, 4, 6 correct
12, 8, 9, 3, 6
Let me explain:
In all the other cases the sequence can not appear during the binary search. For
example, in the first case 10 can not appear after 9, because 9 is greater than q = 6,
so the search will be continued among the numbers which are not greater than 9.

Question 5
 Bookmark this page

Question 11
0.0/1.0 point (ungraded)
Assume that it is needed to find where to insert an element into a sorted array of
integers. Which tools from standard libraries may be useful?

bsearch from the C library


std::lower_bound from the C++ library correct
std::upper_bound from the C++ library correct
std::equal_range from the C++ library correct
std::binary_search from the C++ library
java.util.Arrays.binarySearch from the Java library correct
incorrect
Let me explain:
bsearch searches for the element and returns NULL if it is not found, so it gives no
information considering where the element may be inserted. Similarly,
std::binary_search just answers whether the element is in the array or not.
Java’s binarySearch returns a negative value in the case the element is not present
in the array, and this value uniquely determines where this element could be
inserted. std::lower_bound of C++ will return an iterator, pointing to the element if it
exists, or pointing to the next largest element if it does not exist - which is precisely
where the element could be inserted. In the case the element does not exist,
std::upper_bound will return the same iterator as std::lower_bound, and
std::equal_range just returns a pair of iterators - the one for lower_bound, and
another one for upper_bound.

You might also like