Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) on “Recursive Insertion Sort”.
1. Which of the following is an advantage of recursive insertion sort over its iterative version? Answer: d 2. Insertion sort is an online sorting algorithm. Answer: a 3. What will be the recurrence relation of the code of recursive insertion sort? 4. Which of the following sorting algorithm is stable? 5. Which of the following is a variant of insertion sort? Answer: b 6. Recursive insertion sort is a comparison based sort. Answer: a 7. What is the average case time complexity of recursive insertion sort? Answer: c 8. What is the best case time complexity of recursive insertion sort? Answer: a 9. What is the worst case time complexity of recursive insertion sort? Answer: c 10. How many swaps will be required in the worst case to sort an array having n elements using binary insertion sort? Answer: d 11. What will be the base case for the code of recursive insertion sort ? b) c) d) Answer: c 12. What is the auxiliary space complexity of recursive insertion sort? Answer: b 13. Which of the following is an adaptive sorting algorithm? Answer: a 14. Which of the following sorting algorithm is in place? Answer: a 15.Choose the correct function for recursive insertion sort? b) c) d) Answer: a & Algorithms. and Answers.
a) it has better time complexity
b) it has better space complexity
c) it is easy to implement
d) it has no significant advantage
Clarification: Recursive insertion sort has no significant advantage over iterative insertion sort. It is just a different way to implement the same.
a) true
b) false
Clarification: Insertion sort does not require the entire input data in the beginning itself in order to sort the array. It rather creates a partial solution in every step, so future elements are not required to be considered. Hence it is an online sorting algorithm.
a) T(n) = 2T(n/2) + n
b) T(n) = 2T(n/2) + c
c) T(n) = T(n-1) + n
d) T(n) = T(n-1) + c
Answer: c
Clarification: The recurrence relation of the code of recursive insertion sort is T(n) = T(n-1) + n. It can be solved by the method of substitution and is found to be equal to n2.
a) Selection sort
b) Quick sort
c) Insertion sort
d) Heap sort
Answer: c
Clarification: Out of the given options insertion sort is the only algorithm which is stable. It is because the elements with identical values appear in the same order in the output array as they were in the input array.
a) selection sort
b) shell sort
c) odd-even sort
d) stupid sort
Clarification: Shell sort is a variation of insertion sort. It has a better running time in practical applications.
a) True
b) False
Clarification: In insertion sort, we need to compare elements in order to find the minimum element in each iteration. So we can say that it uses comparisons in order to sort the array. Thus it qualifies as a comparison based sort.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
View Answer
Clarification: The overall recurrence relation of recursive insertion sort is given by T(n) = T(n-1) + n. It is found to be equal to O(n2).
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
View Answer
Clarification: The best case time complexity of recursive insertion sort is O(n). It occurs in the case when the input is already/almost sorted.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
View Answer
Clarification: The overall recurrence relation of recursive insertion sort is given by T(n) = T(n-1) + n. It is found to be equal to O(n2).
a) n
b) 1
c) n * log n
d) log n
View Answer
Clarification: In a normal insertion sort at most n comparisons are required to sort the array. But if we also implement the concept of a binary sort in insertion sort then we can sort by having log n comparisons only.
a) if(n
if(n == 0)
return;
if(n
If(n == 2)
return;
Clarification: The most appropriate condition for the base case of recursive insertion sort is when n is less than or equal 1 then return. It is because we know that an array with only 1 element is always sorted.
a) O(n)
b) O(1)
c) O(n log n)
d) O(n2)
Clarification: The auxiliary space required by recursive insertion sort is O(1). So it qualifies as an in place sorting algorithm.
a) recursive insertion sort
b) merge sort
c) heap sort
d) selection sort
Clarification: Insertion sort is an adaptive algorithm. It is because the time complexity of the algorithm improves when the input array is almost sorted.
a) recursive insertion sort
b) merge sort
c) radix sort
d) counting sort
Clarification: Out of the given options recursive insertion sort is the only algorithm which is in place. It is because the auxiliary space required by recursive bubble sort is O(1).
a)void RecInsertionSort(int arr[], int n)
{
if (n <= 1)
return;
RecInsertionSort( arr, n-1 );
int key = arr[n-1];
int j = n-2;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
void RecInsertionSort(int arr[], int n)
{
if (n < 1)
return;
RecInsertionSort( arr, n-1 );
int key = arr[n-1];
int j = n-2;
while (j >= 0 || arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j] = key;
}
void RecInsertionSort(int arr[], int n)
{
if (n <1)
return;
RecInsertionSort( arr, n-1 );
int key = arr[n-1];
int j = n-2;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j] = key;
}
void RecInsertionSort(int arr[], int n)
{
if (n <= 1)
return;
RecInsertionSort( arr, n-1 );
int key = arr[n-1];
int j = n-1;
while (j >= 0 || arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
Clarification: The base case of recursive bubble sort should be when n equal or less than 1 then return. Also we need to insert the element being chosen as key at its correct position in the sorted array to its left. All this needs to be done in a recursive code.