Data Structure MCQ - Sorting
This section focuses on the "Sorting" of the Data Structure. These Multiple Choice Questions (mcq) should be practiced to improve the Data Structure skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What is recurrence for worst case of QuickSort and what is the time complexity in Worst case?
A. Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n^2)
B. Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n^2)
C. Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nLogn)
D. Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nLogn)
View Answer
Ans : B
Explanation: No explanation.
2. Which of the following is not a stable sorting algorithm?
A. Insertion sort
B. Selection sort
C. Bubble sort
D. Merge sort
View Answer
Ans : B
Explanation:Selection sort is not a stable sorting algorithm.
3. What is an external sorting algorithm?
A. Algorithm that uses tape or disk during the sort
B. Algorithm that uses main memory during the sort
C. Algorithm that involves swapping
D. Algorithm that are considered ‘in place’
View Answer
Ans : A
Explanation: As the name suggests, external sorting algorithm uses external memory like tape or disk.
4. If the number of records to be sorted is small, then …… sorting can be efficient.
A. Merge
B. Heap
C. Selection
D. Bubble
View Answer
Ans : C
Explanation:Selection sorting can be efficient.
5.Suppose we have a O(n) time algorithm that finds median of an unsorted array. Now consider a QuickSort implementation where we first find median using the above algorithm, then use median as pivot. What will be the worst case time complexity of this modified QuickSort.
A. O(n^2 Logn)
B. O(n^2)
C. O(n Logn Logn)
D. O(nLogn)
View Answer
Ans : D
Explanation:If we use median as a pivot element, then the recurrence for all cases becomes T(n) = 2T(n/2) + O(n) The above recurrence can be solved using Master Method. It falls in case 2 of master method.
6. Which of the following is not an in-place sorting algorithm?
A. Selection sort
B. Heap sort
C. Quick sort
D. Merge sort
View Answer
Ans : D
Explanation: Merge sort is not an in-place sorting algorithm.
7. What is the advantage of bubble sort over other sorting techniques?
A. It is faster
B. Consumes less memory
C. Detects whether the input is already sorted
D. All of the mentioned
View Answer
Ans : C
Explanation: Bubble sort is one of the simplest sorting techniques and perhaps the only advantage it has over other techniques is that it can detect whether the input is already sorted.
8.The complexity of sorting algorithm measures the …… as a function of the number n of items to be sorter.
A. average time
B. running time
C. average-case complexity
D. case-complexity
View Answer
Ans : B
Explanation:The complexity of sorting algorithm measures the running time as a function of the number n of items to be sorter.
9. Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this:
2 5 1 7 9 12 11 10
Which statement is correct?
A. The pivot could be either the 7 or the 9.
B. The pivot could be the 7, but it is not the 9
C. The pivot is not the 7, but it could be the 9
D. Neither the 7 nor the 9 is the pivot.
View Answer
Ans : A
Explanation:7 and 9 both are at their correct positions (as in a sorted array). Also, all elements on left of 7 and 9 are smaller than 7 and 9 respectively and on right are greater than 7 and 9 respectively.
10.Consider the situation in which assignment operation is very costly. Which of the following sorting algorithm should be performed so that the number of assignment operations is minimized in general?
A. Insertion sort
B. Selection sort
C. Heap sort
D. None
View Answer
Ans : B
Explanation:Selection sort.
11. In the following scenarios, when will you use selection sort?
A. The input is already sorted
B. A large file has to be sorted
C. Large values need to be sorted with small keys
D. Small values need to be sorted with large keys
View Answer
Ans : C
Explanation: Selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort.
12. What is the worst case complexity of selection sort?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans :DC
Explanation: Selection sort creates a sub-list, LHS of the ‘min’ element is already sorted and RHS is yet to be sorted. Starting with the first element the ‘min’ element moves towards the final element.
13. What is the advantage of selection sort over other sorting techniques?
a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique
View Answer
Ans : A
Explanation: Since selection sort is an in-place sorting algorithm, it does not require additional storage.
14. What is the average case complexity of selection sort?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans : D
Explanation: In the average case, even if the input is partially sorted, selection sort behaves as if the entire array is not sorted. Selection sort is insensitive to input.
15. What is the disadvantage of selection sort?
A. It requires auxiliary memory
B. It is not scalable
C. It can be used for small keys
D. None of the mentioned
View Answer
Ans : B
Explanation: As the input size increases, the performance of selection sort decreases.
16. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are,
A. 5 and 4
B. 4 and 5
C. 2 and 4
D. 2 and 5
View Answer
Ans : A
Explanation: Since the input array is not sorted, bubble sort takes 5 iterations and selection sort takes 4(n-1) iterations.
17. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are,
A. 5 and 4
B. 1 and 4
C. 0 and 4
D. 4 and 1
View Answer
Ans : B
Explanation: Selection sort is insensitive to input, hence 4(n-1) iterations. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.
18.What is the best case complexity of selection sort?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans : D
Explanation: The best, average and worst case complexities of selection sort is O(n2).
(n-1) + (n-2) + (n-3) + …. + 1 = (n(n-1))/2 ~ (n2)/2.
19. What is an internal sorting algorithm?
A. Algorithm that uses tape or disk during the sort
B. Algorithm that uses main memory during the sort
C. Algorithm that involves swapping
D. Algorithm that are considered ‘in place’
View Answer
Ans : B
Explanation: As the name suggests, internal sorting algorithm uses internal main memory.
20.What is the worst case complexity of bubble sort?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans : D
Explanation: Bubble sort works by starting from the first element and swapping the elements if required in each iteration.
21. What is the average case complexity of bubble sort?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans : D
Explanation: Bubble sort works by starting from the first element and swapping the elements if required in each iteration even in the average case.
22. The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements. How many iterations will be done to sort the array?
A. 4
B. 2
C. 1
D. 0
View Answer
Ans : A
Explanation: Even though the first two elements are already sorted, bubble sort needs 4 iterations to sort the given array.
23.What is the best case efficiency of bubble sort in the improvised version?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
View Answer
Ans : B
Explanation: Only 2 elements in the given array are not sorted, hence only 2 iterations are required to sort them.
24. QuickSort can be categorized into which of the following?
A. Brute Force technique
B. Divide and conquer
C. Greedy algorithm
D. Dynamic programming
View Answer
Ans : B
Explanation: First you divide(partition) the array based on the pivot element and sort accordingly.
25. What is a randomized QuickSort?
A. The leftmost element is chosen as the pivot
B. The rightmost element is chosen as the pivot
C. Any element in the array is chosen as the pivot
D. A random number is generated which is used as the pivot
View Answer
Ans : C
Explanation: QuickSort is randomized by placing the input data in the randomized fashion in the array or by choosing a random element in the array as a pivot.
Also Check :
Discussion