Data Structures & Algorithms Multiple Choice Questions on “Timsort”.
1. Which of the following is Python’s standard sorting algorithm? Answer: d 2. Which of the following sorting algorithm is a constituent of tim sort? 3. Tim sort begins sorting the given array by using which of the following sorting algorithm? Answer: c 4. Which of the following sorting algorithm is stable? Answer: a 5. Which of the following sorting algorithm is not in-place? Answer: b 6. Tim sort is a comparison based sort. Answer: a 7. What is the best case time complexity of Tim sort? Answer: a 8. What is the worst case time complexity of Tim sort? Answer: b 9. What is the average time complexity of Tim sort? Answer: b 10. What is the auxiliary space requirement of Tim sort? Answer: a 11. Which of the following algorithm is implemented internally in java when we use function arrays.sort()? Answer: c 12. Why is insertion sort preferred over other sorting algorithms (like selection sort, bubble sort etc.) for Tim sort implementation? Answer: a 13. In which case will tim sort will work as an insertion sort? 14. What is the usual size of a run in tim sort? Answer: c 15. What will be the output of the given Java code? a) [4,5,7,8,9,10] Answer: a 16. What will be the output of the given Java code? a) [4,5,7,8,9,10] Answer: d & Algorithms. and Answers.
a) quick sort
b) introsort
c) merge sort
d) tim sort
Clarification: Tim sort has been python’s standard sorting algorithm since its version 2.3. It is an example of hybrid sorting algorithm which means it uses more than one sorting algorithm as a routine.
a) selection sort
b) quick sort
c) merge sort
d) heap sort
Answer: c
Clarification: Tim sort is a hybrid sorting algorithm which means it uses more than one sorting algorithm as a routine. It is derived from insertion sort and merge sort.
a) selection sort
b) quick sort
c) insertion sort
d) merge sort
Clarification: Tim sort begins sorting any given array by using insertion sort for each run. The array is divided into smaller parts for this purpose, each part having a size equal to value of run. Then these small parts called runs are merged in order to obtain sorted array.
a) Tim sort
b) Introsort
c) Quick sort
d) Heap sort
Clarification: Out of the given options Tim sort is the only algorithm which is stable. As both constituents of Tim sort (I.e insertion sort and merge sort) are stable so Tim sort also becomes stable.
a) insertion sort
b) tim sort
c) quick sort
d) intro sort
Clarification: Tim sort is not an in-place sorting algorithm as it requires auxiliary space. It is because it requires to merge sorted runs which requires a third array of the size equal to the sum of the two runs.
a) true
b) false
Clarification: Merge sort and insertion sort are comparison based sorts. Thus overall Tim sort also becomes a comparison based sort.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Best case time complexity of Tim sort occurs when the input array is already sorted. In such a case only one run will be required.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Worst case time complexity of Tim sort is O(n log n). It is because the worst complexity of merge sort is O(n log n) and insertion sort is only applied for small arrays.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Average time complexity of Tim sort remains to be O(n log n). It is the same as the average case complexity of merge sort.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
View Answer
Clarification: Tim sort is a hybrid of merge sort and insertion sort. It requires to merge sorted runs which require a third array of the size equal to the sum of the two runs. So in worst case the auxiliary space requirement will be O(n).
a) intro sort
b) quick sort
c) tim sort
d) merge sort
Clarification: Java makes use of Tim sort internally for implementing arrays.sort(). It is mainly due to the fastness of this algorithm in comparison to other comparison based sorts.
a) Because insertion sort is faster and adaptive
b) Because insertion sort requires less space
c) Because insertion sort is easy to implement
d) Because insertion sort is easy to understand
Clarification: When small arrays need to be sorted then insertion sort proves to be the best choice. Also, it is adaptive so it performs better than others when the given array is fully/partially sorted.
a) when no. of elements are less than 64
b) when no. of elements are greater than 64
c) when no. of elements are less than size of run
d) when no. of elements are less than 32
Answer: c
Clarification: Tim sort uses a hybrid of insertion and merge sort. It reduces to insertion sort when the size of array is less than the size of run as insertion sort is efficient in sorting small arrays.
a) 32
b) less than 32
c) 32-64 depending on size of the array
d) 64
Clarification: Usually the size of the run is chosen somewhere between 32 and 64. The size of run is preferably chosen in powers of 2 in order to maintain balance while merging the sorted runs.import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
// Our arr contains 8 elements
int[] arr = {10,7,9,5,8,4};
Arrays.sort(arr);
System.out.printf(Arrays.toString(arr));
}
}
b) [10,9,8,7,5,4]
c) 4,5,7,8,9,10
d) error
Clarification: The given program sorts the input in ascending order by using the function Arrays.sort(). It uses Tim sort internally.import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
int[] arr = {10,7,9,5,8,4};
Arrays.sort(arr, 1, 3);
System.out.printf(Arrays.toString(arr));
}
}
b) [10,9,8,7,5,4]
c) [10,5,7,8,9,4]
d) [10,7,9,5,8,4]
Clarification: The given program sorts only a portion of the input array. It is done by passing two extra arguments to the function Arrays.sort(). It sorts the elements between index 1 and 2.