Data Structures & Algorithms Multiple Choice Questions on “Bottom-Up Mergesort”.
1. Merge sort uses which of the following algorithm to implement sorting? Answer: c 2. What is the average case time complexity of standard merge sort? Answer: a 3. What is the auxiliary space complexity of standard merge sort? Answer: c 4. What is the auxiliary space complexity of bottom up merge sort? Answer: b 5. What is the average time complexity of bottom up merge sort? Answer: a 6. Merge sort uses which of the following method to implement sorting? Answer: a 7. Bottom up merge sort uses recursion. Answer: b 8. Bottom up merge sort is a stable sort. 9. Choose the correct statement about bottom up merge sort from the following? 10. Choose the correct option from the following that represents bottom up merge sort function? b) c) d) View Answer Answer: a
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
Clarification: Merge sort uses the technique of divide and conquer in order to sort a given array. It divides the array into two halves and applies merge sort algorithm to each half individually after which the sorted versions of these halves are merged together.
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Clarification: The recurrence relation for merge sort is given by T(n) = 2T(n/2) + n. This can be solved using master’s theorem and is found equal to O(n log n).
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Clarification: The merging of two sorted arrays requires an additional space of n due to which the auxiliary space requirement of merge sort is O(n). Thus merge sort is not an in place sorting algorithm.
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Clarification: The auxiliary space complexity of bottom up merge sort is same as standard merge sort as both uses the same algorithm for merging the sorted arrays which takes o(n) space. But bottom up merge sort does not need to maintain a call stack.
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Clarification: The merge function in the bottom up merge sort takes O(n) time which is placed inside the for loop. The loop runs for O(log n) time, thus the overall time complexity of the code becomes O(n log n).
a) merging
b) partitioning
c) selection
d) exchanging
Clarification: Merge sort implements sorting by merging the sorted versions of smaller parts of the array. Thus its method of sorting is called merging.
a) true
b) false
Clarification: Bottom up merge sort uses the iterative method in order to implement sorting. It begins by merging a pair of adjacent array of size 1 each and then merge arrays of size 2 each in the next step and so on.
a) true
b) false
Answer: a
Clarification: Bottom up merge sort like standard merge sort is a stable sort. This implies that the relative position of equal valued elements in the input and sorted array remain same.
a) bottom up merge sort has greater time complexity than standard merge sort
b) bottom up merge sort has lesser time complexity than standard merge sort
c) bottom up merge sort saves auxiliary space required on call stack
d) bottom up merge sort uses recursion.
Answer: c
Clarification: Bottom up merge sort unlike standard merge sort uses an iterative algorithm for sorting. Thus, it saves auxiliary space required by the call stack.
a)void mergesort(int Arr[], int temp[], int low, int high)
{
for (int m = 1; m <= high - low; m = 2*m)
{
for (int i = low; i < high; i += 2*m)
{
int left = i;
int mid = i + m - 1;
int right = min(i + 2*m - 1, high);
merge(Arr, temp, left, mid, right);
}
}
}
void mergesort(int Arr[], int temp[], int low, int high)
{
for (int m = 1; m <= high - low; m = 2*m)
{
for (int i = low; i < high; i += m)
{
int left = i;
int mid = i + m - 1;
int right = min(i + 2*m - 1, high);
merge(Arr, temp, left, mid, right);
}
}
}
void mergesort(int Arr[], int temp[], int low, int high)
{
for (int m = 1; m <= high - low; m = m)
{
for (int i = low; i < high; i += 2*m)
{
int left = i;
int mid = i + m - 1;
int right = min(i + 2*m - 1, high);
merge(Arr, temp, left, mid, right);
}
}
}
void mergesort(int Arr[], int temp[], int low, int high)
{
for (int m = 1; m <= high - low; m = 2*m)
{
for (int i = low; i < high; i += 2*m)
{
int left = i;
int mid = i + m - 1;
int right = min(i + m - 1, high);
merge(Arr, temp, left, mid, right);
}
}
}
Clarification: Bottom up merge sort uses iterative method in order to implement sorting. It begins by merging a pair of adjacent array of size 1 each and then merge arrays of size 2 each in the next step and so on. The process of merging the sorted arrays is same as in standard merge sort.